"if" statement not responding

Cant get code to work!

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1289 Views - Last Post: 23 June 2008 - 06:01 AM

#1 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

"if" statement not responding

Post icon  Posted 14 June 2008 - 11:14 AM

Hi everyone,

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','')

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: "if" statement not responding

#2 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: "if" statement not responding

Posted 14 June 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 ||.
Was This Post Helpful? 0
  • +
  • -

#3 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 14 June 2008 - 12:56 PM

View Postatdrago, on 14 Jun, 2008 - 11:53 AM, said:

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:

Thanks again for helping me :)
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,144
  • Joined: 07-September 06

Re: "if" statement not responding

Posted 14 June 2008 - 02:30 PM

Try this:

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.
Was This Post Helpful? 0
  • +
  • -

#5 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 15 June 2008 - 02:42 AM

View PostBetaWar, on 14 Jun, 2008 - 02:30 PM, said:

Try this:

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 :)


Thanks BetaWar for your help :^:
Was This Post Helpful? 0
  • +
  • -

#6 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 15 June 2008 - 04:06 AM

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:
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,144
  • Joined: 07-September 06

Re: "if" statement not responding

Posted 15 June 2008 - 01:10 PM

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:

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.
Was This Post Helpful? 0
  • +
  • -

#8 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 15 June 2008 - 01:32 PM

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','')

//
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? :)
Was This Post Helpful? 0
  • +
  • -

#9 kelbly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-June 08

Re: "if" statement not responding

Posted 15 June 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
Was This Post Helpful? 0
  • +
  • -

#10 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 15 June 2008 - 02:16 PM

View Postkelbly, on 15 Jun, 2008 - 01:49 PM, said:

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'


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: :)
Was This Post Helpful? 0
  • +
  • -

#11 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,144
  • Joined: 07-September 06

Re: "if" statement not responding

Posted 15 June 2008 - 02:42 PM

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:

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!');
}
}

Was This Post Helpful? 0
  • +
  • -

#12 kelbly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-June 08

Re: "if" statement not responding

Posted 15 June 2008 - 02:44 PM

View Postbaggins, on 15 Jun, 2008 - 02:16 PM, said:

View Postkelbly, on 15 Jun, 2008 - 01:49 PM, said:

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'


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: :)



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!!
Was This Post Helpful? 0
  • +
  • -

#13 baggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-June 08

Re: "if" statement not responding

Posted 16 June 2008 - 03:54 AM

hi Kelbly,

I just check mine and I didn't translate it, I just wrote it, that's all. I will have no clue how to do it, so far I think that project is very difficult anyway.

:)



Thanks BetaWar, again you save the day! it works fine :D
Was This Post Helpful? 0
  • +
  • -

#14 cavvysri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 12-June 08

Re: "if" statement not responding

Posted 16 June 2008 - 06:34 AM

View Postkelbly, on 15 Jun, 2008 - 02:44 PM, said:

View Postbaggins, on 15 Jun, 2008 - 02:16 PM, said:

View Postkelbly, on 15 Jun, 2008 - 01:49 PM, said:

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'


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: :)



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!!


seems like were all on the same course lol, im at the same point as you Kelbly, its driving me mad too, ive tried all sorts
Was This Post Helpful? 0
  • +
  • -

#15 cavvysri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 12-June 08

Re: "if" statement not responding

Posted 16 June 2008 - 06:46 AM

hi Kelbly, seems alot of us are on the same course on here lol...ve tried various things to display whether a book is reserved etc but no luck as yet....its driving me mad, wasted days trying to do it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2