Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Problem

Options
  • 01-04-2007 7:37pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    Basically I'm learning JavaScript and as I progress through the book ('Javascript Demystified') I'm building small programs. I've built two programs that (are supposed to) do the same thing but in a different way. The first way uses if, if...else etc. statments and the second one a 'for' loop.
    Heres my first program (if, if...else etc.)
    <html>
    <head>
    <title></title>
    <script language="javascript">
    
    //			Name Your Variables
    
    var orgNumber
    var testNumber
    
    //			Explain the Boolean system(1)
    
    	alert('This script uses the Boolean system meaning 1=True and 0=False')
    
    //			Ask for a number(2)
    
    	orgNumber=prompt('Please enter a number between 1 and 5','');
    
    //			If the number is too big refuse it(3 A)
    
    	if (orgNumber>5){
    		alert('This number is too big')
    			}
    
    
    //			If the number is too small refuse it(3 B)
    
    	else if (orgNumber<1){
    		alert('This number is too small')
    			     }
    
    
    //			If everything is OK continue(4)
    
    	else{
    
    
    //			Ask if it smaller than 3(5)
    
    		testNumber=prompt('Is your number smaller than 3?','1=True, 0=False');
    
    
    //			If its bigger than 3...(6)
    
    		if (testNumber==0){
    
    
    //			Ask if it smaller than 4(7)
    
    			testNumber=prompt('Is your number smaller than 4?','1=True, 0=False');
    
    
    //			If it is smaller than 4 tell them its 3(8)
    
    				if (testNumber==1){
    					document.write('Your number is 3')
    						  }
    
    
    //			Otherwise ask them if its smaller than 5(9)
    
    				else if (testNumber==0){
    					testNumber=prompt('Is your number smaller than 5?','1=True, 0=False');
    
    
    //			If its is tell them its 4(10)
    
    					if (testNumber==1){
    						document.write('Your number is 4')
    							  }
    
    
    //			If its not tell them its 5(11)
    
    					else {
    						document.write('Your number is 5')
    					     }
    							}
    				    }
    
    
    //			If the number IS smaller than 3 (12)
    
    //	NOTE: The previous french brace ( } ) closed the entire 'else' block
    
    		else {
    
    
    //			Ask if its smaller than 2(13)
    
    			testNumber=prompt('Is your Number smaller than 2?','1=True, 0=False');
    
    
    //			If its not tell them its 2(14)
    
    			if (testNumber==0) {
    				document.write('Your number is 2')
    					   }
    
    //			Otherwise tell them its 1(15)
    
    			else {
    				document.write('Your number is 1')
    			     }
    		     }
    
    	     }
    //			Make sure there telling the truth and if not call them on it.
    //					NOT WORKING
    
    		if (orgNumber!=testNumber) {
    			document.write('. You made a mistake, your original number was ' + orgNumber)
    					   }
    		else {
    			document.write('')
    		     }
    
    
    
    </script>
    </head>
    <body>
    <noscript>
    <center>
    This is how it should work<br>
    (x)=Source Code Reference <p><b>
    Explain Boolean System(1)<br>
    	Ask for a number(2)<br>
    		If the number is too small or big refuse it(3 A,B)<br>
    			If everything is OK continue(4)<br>
    				Ask if it smaller than 3(5)<br>
    </b></center>
    If its bigger than 3...(6)<p>
    <b><center>
    	Ask if it smaller than 4(7)<br>
    		If it is smaller than 4 tell them its 3(8)<br>
    			Otherwise ask them if its smaller than 5(9)<br>
    				If its is tell them its 4(10)<br>
    					If its not tell them its 5(11)
    </b></center>
    <p>
    	If the number IS smaller than 3(12)<p>
    <b><center>
    	Ask if its smaller than 2(13)<br>
    		If its not tell them its 2(14)<br>
    			Otherwise tell them its 1(15)<br>
    </b></center>
    </noscript>
    
    
    </body>
    </html>
    

    This one tells you you selected the wrong number even when you haven't. What am I doing wrong?


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    You seem to be trying to compare an integer with a boolean value in your last if statement.

    Probably what you want to do is add another variable and when you print a guess to the screen assign the guess to the new variable. Then in your last if statement you compare the original number to the number guessed. As an example...
    				if (testNumber==1){
    					document.write('Your number is 3')
    					[COLOR="Red"]guessedNum = 3[/COLOR]
    					}
    

    And then your last if statement should look like this;
    
    		if (orgNumber!=[COLOR="red"]guessedNum[/COLOR]) {
    			document.write('. You made a mistake, your original number was ' + orgNumber)
    					   }
    		else {
    			document.write('')
    		     }
    
    


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Works perfectly, thanks. As I say I'm still learning so I have another question, can you make a boolean a prompt? If so, how?

    Heres my second script. It uses the for loop (which I'm having slight problems understanding, hence this problem). The script doesn't seem to work at all.
    <html>
    <head>
    <title></title>
    <script language="javascript">
    
    
    //            Variable List
    var number
    var number2
    var number3
    var i
    var j
    var k
    
    //           Begin
    number=prompt('Enter a number','Enter HERE');
    
    
    //           Number Specifications
    if (number>5) {
    	alert('Your number is too big')
    }else if (number<1) {
    	alert('Your number is too small')
    	}
    
    //          Guess begin
    else{
    
    //         Set K
     k=0
    
    //         Begin collecting info
    	number2=prompt('Is your number less than 3? ','');
    
    //         Begin Loop
    	for(i=1;i<3;i++) {
    
    //         Set J
    	j=3
    
    //	   If Number is right alert us
    	if(number==k) {
    		alert('The number is ' + k)
    		i=4
    
    
    	if (number2==0) {
    k=j-i
    			//alert('K is ' + k)
    		number3=prompt('Is your number less than ' + (j+i) + '?','');
    
    			//alert('K is '+ k)
    	}else {
    		if (number2==1) {
    k=j+1
    			//alert('K is ' + k)
                    number3=prompt('Is your number less than ' + (j-i) + '?','');
            	//k=j-i
    			//alert('K is ' + k)
    	     	}
          	      }
    
    	}
    	if(number==k) {
    		alert('The number is ' + k)
    	}
          }
           }
    </script>
    </head>
    <body>
    </body>
    </html>
    

    I've put alerts in there to make sure the loop is being acknowledge but because it doesn't even start I don't know. At the moment the loops are commented out.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Seachmall wrote:
    Works perfectly, thanks. As I say I'm still learning so I have another question, can you make a boolean a prompt? If so, how?

    You mean a "Yes/No" popup?

    The closest thing is the "confirm" dialog, which gives you an OK/Cancel popup.
    var myConfirm = confirm("is your number less than 5?");
    
    if (myConfirm)
    {
        //user has pressed OK
    }
    else
    {
        //user has pressed cancel
    }
    


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    To rephrase: can you make a boolean variable? I will be trying that confirm box though, thanks.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Seachmall wrote:
    To rephrase: can you make a boolean variable? I will be trying that confirm box though, thanks.

    you can do this:
    var mybool = new Boolean(false);
    

    And check for it like this:
    if (mybool == true)
    {
    	alert("true");
    }
    

    just doing this:
    if (mybool)....
    

    checks to see if the object is not null, so will always give a true value once the variable is declared (strangely, even if you pass null in the parentheses).

    Edit: I have quite a handy JavaScript help file in CHM format - I can upload it somewhere for you?


  • Advertisement
  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Yeah, that would be great. Upload anywhere handy for you and pm me or post the link in this thread. Thanks.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Here you go.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Thanks but I doesn't work. Every Time I try to open a folder it says 'This program cannot display the webpage'. Odd. Thanks anyway.


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    I reference to my 'for' loop script problem I fixed it by making it a 'while' loop. I'm not sure if I could even have done it using a 'for' loop. For anyone interested heres my source
    <html>
    <head>
    <title></title>
    <script language="javascript">
    var orgNumber
    var guessNumber
    var i
    var j
    
    orgNumber = prompt('Please enter a number between 1 and 5','');
    	if (orgNumber>5){
    		alert('This number is too big, please press F5')
    					}
    	else if (orgNumber<1){
    		alert('This number is too small, please press F5')
    						 }
    	else{
    		guessNumber = prompt('Is your number bigger than 3?','1=Yes,2=no');
    			if (guessNumber == 1){
    			i=4
    			j=4
    				while(i!=orgNumber){
    					guessNumber = prompt('Is your number bigger than ' + j +'?','');
    					i++
    					j++
    								   }
    						if (i==orgNumber){
    							alert('Your number is ' + orgNumber)}
    						else{}
    								}
    			else if(guessNumber == 0){
    				i=3
    				j=3
    				while(i!=orgNumber){
    					guessNumber = prompt('Is your number less than ' + j + '?','');
    					i--
    					j--
    									}
    						if (i==orgNumber){
    							alert('Your number is ' + orgNumber)}
    						else{}
    								}
    		 }
    
    
    
    
    </script>
    </head>
    <body>
    </body>
    </html>
    

    EDIT--
    Just noticed that if you try to trick the program that it continues to infinity asking you if your number is bigger/smaller than j. I tried placing a 'while' loop inside the current 'while' loop telling it if k (a new variable I used) goes above/below 5/1 (5 and 1 are its boundries) to alert the user however when I executed this script IE told me to abort it as it will slow or freeze my browser (which is IE obviously). Where am I going wrong... again.

    EDIT--
    Tried placing an 'If' statment with a 'break' and now it works fine, for now. More debugging to be done.


  • Closed Accounts Posts: 119 ✭✭frodo_dcu


    eoin_s wrote:
    Here you go.

    Great reference. Very Handy Eoin.


  • Advertisement
  • Registered Users Posts: 4,780 ✭✭✭JohnK


    eoin_s wrote:
    Here you go.
    Cheers for that Eoin, such things are always handy :)


Advertisement