d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript - Easy > What Am I Doing Wrong...
Add Reply New Topic New Poll
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Oct 18 2012 12:15pm
I need a new set of eyes for this, im really new to javascript and ive been working on this lab for like 3 hrs and i cant make it repeat - basically i must be doing something easy wrong here is the lab:

Quote
Write a program that asks a user to enter a number. If the number is greater than a starting variable that is set to zero, output the number and then ask the user to enter a bigger number and store it in the starting variable. As long as the newly entered number is bigger than the last one then keep asking the user to enter another bigger number. Display each number that is entered. Once the user enters a number that is smaller than the last one entered then display the sign off message "Good bye!"


Code
function lab09unknownLoopCountPart1() {

// declare variables // constants

   var enteredNumber;
   var startingNumber;
   var previousNumber;
   var test;
   
   startingNumber = 0;
   enteredNumber = 1;

// get input & prompt user for input // loop

   while (startingNumber < enteredNumber) {
       enteredNumber = Number(prompt("Please enter a number"));
       previousNumber = startingNumber;
       if (startingNumber < enteredNumber) {
           document.write(enteredNumber + " is bigger than " + previousNumber);
           startingNumber = enteredNumber;
       } else {
           document.write(enteredNumber + " is smaller than " + previousNumber
               + "<br/>" + "Good Bye!");
       }
   }
}


Mind my indentation - its a bit off right now i plan on fixn it last. also ignore the test var. Yah to be honest staring at this to long just gettin annoyed any advice is good :D -- keep in mind this is for a class so like do keep in mind we probably havnt gone over the easier more advanced / easier ways to do this so...
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 18 2012 03:57pm
I found your problem I think

The first thing that happens in your program is the while loop.

I will run an example for you:

Start = 0
entered = 1

while (0 < 1) // works fine

entered = 10 // for example

previous = 0

if (0 < 10)

print "10 is bigger than 0"

start = 10

~_~_~_~_~_~_~_~_~_~ // next loop interation begins

while (10 < 10)

10 is NOT less than 10. This is your problem. The next iteration won't start at all because the WHILE check happens BEFORE user input.

This post was edited by Eep on Oct 18 2012 03:58pm
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Oct 18 2012 04:34pm
Quote (Eep @ Oct 18 2012 04:57pm)
I found your problem I think

The first thing that happens in your program is the while loop.

I will run an example for you:

Start = 0
entered = 1

while (0 < 1) // works fine

entered = 10 // for example

previous = 0

if (0 < 10)

print "10 is bigger than 0"

start = 10

~_~_~_~_~_~_~_~_~_~ // next loop interation begins

while (10 < 10)

10 is NOT less than 10. This is your problem. The next iteration won't start at all because the WHILE check happens BEFORE user input.


Brilliant... I understand what u mean now by while check happens before user input... i cant believe i never saw that... So does this mean i would use a " for " statement? -- Thanks btw for help ill be evening ur fg out :D

Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 18 2012 04:55pm
Quote (Noobtard @ Oct 18 2012 05:34pm)
Brilliant... I understand what u mean now by while check happens before user input... i cant believe i never saw that... So does this mean i would use a " for " statement? -- Thanks btw for help ill be evening ur fg out :D


If possible, I would suggest looping the user input

like while (..//prompts for user to input a number into a variable)

{

do stuff

}

this way it keeps looping as long as there are inputs from the user. The loop will stop (obviously) once the user enters the smaller number because you can add a break statement or just end the program I think.

This post was edited by Eep on Oct 18 2012 04:57pm
Member
Posts: 644
Joined: Oct 27 2012
Gold: 1,404.00
Oct 27 2012 07:53pm
Not really sure why you are using a while loop at all.

You could completely eliminate the start number variable if you just used a do { .. } while loop

do
{
// take your input, etc etc
} while (input > previous)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll