Welcome to Dream.In.Code
Getting Help is Easy!

Join 117,616 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,962 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



"if" statement not responding

2 Pages V  1 2 >  
Reply to this topicStart new topic

"if" statement not responding, Cant get code to work!

baggins
post 14 Jun, 2008 - 11:14 AM
Post #1


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions


Hi everyone,

I'm new on the website smile.gif
My problem is that I have my first 'if' statement working fine but the second is not responding properly. Instead of writing: no such book, it wrote book reserved+no such book

can someone please help me

Here is my coding:

var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon'];
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]

var IndexBookArray;
IndexBookArray = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (IndexBookArray == 1)

IndexBookArray = window.prompt('Enter the index number of the book to be reserved','')

if((IndexBookArray == 0) || (IndexBookArray == 2) ||(IndexBookArray > 3) || (IndexBookArray < 6))
{
document.write('Book reserved')
}

if (IndexBookArray > 5)
{
document.write('--->' + 'No such book!')
}
User is offlineProfile CardPM

Go to the top of the page


atdrago
post 14 Jun, 2008 - 11:53 AM
Post #2


New D.I.C Head

*
Joined: 8 Jun, 2008
Posts: 27



Thanked 2 times
My Contributions


I'm not really sure why you first make IndexBookArray an array, and then make it an integer... But anyway, if IndexBookArray is equal to 7, here's what will happen.

Your first if statement will say: No it's not equal to 0, No it's not equal to 2, Yes it's greater than 3, No it's not less than 6. So that will return true.

Then your next if statement will say : Yes it's greater than 5. So that will return true.

Consider using the && operator instead of ||.
User is offlineProfile CardPM

Go to the top of the page

baggins
post 14 Jun, 2008 - 12:56 PM
Post #3


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions


QUOTE(atdrago @ 14 Jun, 2008 - 11:53 AM) *

I'm not really sure why you first make IndexBookArray an array, and then make it an integer... But anyway, if IndexBookArray is equal to 7, here's what will happen.

Your first if statement will say: No it's not equal to 0, No it's not equal to 2, Yes it's greater than 3, No it's not less than 6. So that will return true.

Then your next if statement will say : Yes it's greater than 5. So that will return true.

Consider using the && operator instead of ||.


I do it wrong, the thing is, there is 6 books stored in a library.

If the reader pick book 0,2,4 and 5 in the bookArray, a message say: book reserved. If the reader pick book 1, a message say: you cannot select that book. If the reader select an index that is not on the list, it say: no such book.

I send you my coding with true and false:

var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon'];

var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]



var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (userChoice == 1)
{
window.prompt('Enter the index number of the book to be reserved','')
}
var isRight = true;
if((userChoice == 0) && (userChoice == 2) && (userChoice > 3) && (userChoice < 6))
{
document.write('Book reserved');
}
else
{
if(userChoice > 5);
{
document.write('--->' + 'No such book!');
isRight = true
}
}

I have difficulties with true and false, I don't know when to use them. blink.gif

Thanks again for helping me smile.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 14 Jun, 2008 - 02:30 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,569



Thanked 62 times

Dream Kudos: 975
My Contributions


Try this:

CODE
var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]



var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (userChoice == 1)
{
userChoice = window.prompt('Enter the index number of the book to be reserved','')
}

if(userChoice == 1){
document.write("You can't select that book.");
}
if((userChoice == 0) || (userChoice == 2) || ((userChoice >= 3) && (userChoice < 6)))
{
document.write('Book reserved');
}
else
{
if(userChoice > 5){
document.write('--->' + 'No such book!');
}
}


Before you were telling it to say true (the if statement passed) when the book choice was 0, and was 2 (never going to happen since only one of those can be true at a time) and when the book choice was greater than 3 and less than 6. Basically you said that there had to be some major problems going on with the space time continuem for this if statement to be true.

Then the semi-colon after the if(userChoice > 5) makes it output No Such Book for everything.
User is offlineProfile CardPM

Go to the top of the page

baggins
post 15 Jun, 2008 - 02:42 AM
Post #5


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions


QUOTE(BetaWar @ 14 Jun, 2008 - 02:30 PM) *

Try this:

CODE
var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]



var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (userChoice == 1)
{
userChoice = window.prompt('Enter the index number of the book to be reserved','')
}

if(userChoice == 1){
document.write("You can't select that book.");
}
if((userChoice == 0) || (userChoice == 2) || ((userChoice >= 3) && (userChoice < 6)))
{
document.write('Book reserved');
}
else
{
if(userChoice > 5){
document.write('--->' + 'No such book!');
}
}


Before you were telling it to say true (the if statement passed) when the book choice was 0, and was 2 (never going to happen since only one of those can be true at a time) and when the book choice was greater than 3 and less than 6. Basically you said that there had to be some major problems going on with the space time continuem for this if statement to be true.

Then the semi-colon after the if(userChoice > 5) makes it output No Such Book for everything.


Perferct, it works smile.gif


Thanks BetaWar for your help icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

baggins
post 15 Jun, 2008 - 04:06 AM
Post #6


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions


I put option 2 for the userchoice, which is select ''2. for borrow books''. Well there it give me the same result as if I pick book 2 and write book reserved. Do I have to give an another variable name?

rolleyes.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 15 Jun, 2008 - 01:10 PM
Post #7


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,569



Thanked 62 times

Dream Kudos: 975
My Contributions


That is probably a good idea. It will make things a lot less confusing and not require some additional checks to make sure that it is running correctly.

YOu could keep the same variables if you wanted to change it to something like so:

CODE
var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]



var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

if (userChoice == 1)
{
userChoice = window.prompt('Enter the index number of the book to be reserved','');
if(userChoice == 1){
document.write("You can't select that book.");
}
if((userChoice == 0) || (userChoice == 2) || ((userChoice >= 3) && (userChoice < 6)))
{
document.write('Book reserved');
}
else
{
if(userChoice > 5){
document.write('--->' + 'No such book!');
}
}
}


I believe that will do what you are wanting.
User is offlineProfile CardPM

Go to the top of the page

baggins
post 15 Jun, 2008 - 01:32 PM
Post #8


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions



I try with a new variable, my coding work fine but I still have 'book reserved'.

Here my new code:

var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon'];
var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]

var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')

//
CODE

if (userChoice == 1)
{
userChoice = window.prompt('Enter the index number of the book to be reserved','')
}

if(userChoice == 1)
{
document.write('--->' + 'You cannot reserve this book!');
}

if((userChoice == 0) || (userChoice == 2) || ((userChoice > 3) && (userChoice < 6)))
{
document.write('--->' + 'Book reserved');
}
else
{
if(userChoice > 5){
document.write('--->' + 'No such book!');
}

if(userChoice == 3)
{
document.write('--->' + 'You cannot reserve this book!');
}
}

var secondChoice;
secondChoice = window.prompt('Enter the index number of the book to be borrowed','')

if  (secondChoice == 1)
{
document.write('--->' + 'You cannot borrow this book!');
}

if((secondChoice == 0) || (secondChoice == 2) || ((secondChoice > 3) && (secondChoice < 6)))
{
document.write('--->' + 'Your book may be collected from the counter');
}
else
{
if(secondChoice > 5){
document.write('--->' + 'No such book!');
}

if(secondChoice == 3)
{
document.write('--->' + 'You cannot borrow this book!');
}
}
//


Any idea why it keep doing it? smile.gif
User is offlineProfile CardPM

Go to the top of the page

kelbly
post 15 Jun, 2008 - 01:49 PM
Post #9


New D.I.C Head

*
Joined: 15 Jun, 2008
Posts: 8

Hi there,
I think we must be doing the same course, I was just wondering if you were able to give me some guidance on question 2 (iii) where we need to display the status of the books?
kind regards
User is offlineProfile CardPM

Go to the top of the page

baggins
post 15 Jun, 2008 - 02:16 PM
Post #10


New D.I.C Head

*
Joined: 14 Jun, 2008
Posts: 14


My Contributions


QUOTE(kelbly @ 15 Jun, 2008 - 01:49 PM) *

Hi there,
I think we must be doing the same course, I was just wondering if you were able to give me some guidance on question 2 (iii) where we need to display the status of the books?
kind regards


Hi Kelbly,

New myself I'm not sure I did it right smile.gif I add my codding just under 'welcome'

CODE


document.write('welcome to the catalogue!')
document.write('<BR>')
function printCharacterBlock(lines, number, outputCharacter)

{
    for (var line = 1; line <= lines; line = line + 1)
    {
        for (var position = 1; position <= number; position = position + 1)
        {
            document.write(outputCharacter)
        };
        document.write('<BR>')  
    }
};

printCharacterBlock(1, 63, '-');
                    document.write('--' + '0' + '---' + bookArray[0] + '--' + authorArray[0] + '---' + 'On shelf' + '<BR>')
//then you keep on to write that line with all the books, which will finish at bookArray[5].


I hope your understand a bit because I'm not very good to explain blink.gif smile.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 15 Jun, 2008 - 02:42 PM
Post #11


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,569



Thanked 62 times

Dream Kudos: 975
My Contributions


You still had it actng out all the if statements for the userChocie also, so it was dong what it was supposed to do, just not what you wanted.

Here ya go:

CODE
var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon'];
var IndexBookArray;
var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]

var userChoice;
userChoice = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')


if (userChoice == 1)
{
secondChoice = window.prompt('Enter the index number of the book to be reserved','')
}

if(secondChoice == 1)
{
document.write('--->' + 'You cannot reserve this book!');
}

if((secondChoice == 0) || (secondChoice == 2) || ((secondChoice > 3) && (secondChoice < 6)))
{
document.write('--->' + 'Book reserved');
}
else
{
if(secondChoice > 5){
document.write('--->' + 'No such book!');
}

if(secondChoice == 3)
{
document.write('--->' + 'You cannot reserve this book!');
}
}
User is offlineProfile CardPM

Go to the top of the page

kelbly
post 15 Jun, 2008 - 02:44 PM
Post #12


New D.I.C Head

*
Joined: 15 Jun, 2008
Posts: 8

QUOTE(baggins @ 15 Jun, 2008 - 02:16 PM) *

QUOTE(kelbly @ 15 Jun, 2008 - 01:49 PM) *

Hi there,
I think we must be doing the same course, I was just wondering if you were able to give me some guidance on question 2 (iii) where we need to display the status of the books?
kind regards


Hi Kelbly,

New myself I'm not sure I did it right smile.gif I add my codding just under 'welcome'

CODE


document.write('welcome to the catalogue!')
document.write('<BR>')
function printCharacterBlock(lines, number, outputCharacter)

{
    for (var line = 1; line <= lines; line = line + 1)
    {
        for (var position = 1; position <= number; position = position + 1)
        {
            document.write(outputCharacter)
        };
        document.write('<BR>')  
    }
};

printCharacterBlock(1, 63, '-');
                    document.write('--' + '0' + '---' + bookArray[0] + '--' + authorArray[0] + '---' + 'On shelf' + '<BR>')
//then you keep on to write that line with all the books, which will finish at bookArray[5].


I hope your understand a bit because I'm not very good to explain blink.gif smile.gif



Hi thanks for that, I kinda had that bit sorted, and I could display the books, and authors but have had a complete mind block at being able to translate whether a book is on the shelf or been borrowed!

its driving me, and the people around me nuts!!
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/8/08 12:00AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month