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!
I'm new on the website 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','')
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.
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.
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.
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.
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?
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 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','') }
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 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 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
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','') }
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 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
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!