1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem with while statement

Discussion in 'JavaScript' started by Richard_K, Nov 20, 2019.

  1. #1
    I'm working on a java course but having some troubles with a question

    The question is
    "
    Your editor currently has a variable declared target to which a random number between 0 and 10 will be assigned. Print it to the console.
    Under it write a small program which will keep guessing numbers and attributing them to the variable guess and printing them until the number guessed is the same as the target. "

    My code is for now:

    ````
    var target = Math.floor(Math.random() * 11);
    var guess = Math.floor(Math.random() * 11);
    
    console.log("Target is " +target);
    console.log("First guess is " +guess);
    
    while (guess!==target) {
    
    guess = Math.floor(Math.random() * 11);  
        console.log('New guess is '+guess);
    }
    
    console.log('Yay! You\'ve done it, '+guess +' is right');
    Code (javascript):
    ````
    The editor is returning the error:
    >>>>Code is incorrect
    Make you you're printing the value of guess to the console in the line below assigning it a new value
    Target is 4
    First guess is 8
    New guess is 2
    New guess is 10
    New guess is 0
    New guess is 10
    New guess is 6
    New guess is 2
    New guess is 0
    New guess is 4
    Yay! You've done it, 4 is right

    Can somebody tell me if my code is good and if not what i must change to get it working?
     
    Last edited by a moderator: Nov 23, 2019
    Richard_K, Nov 20, 2019 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    What "editor"? Why would your editor be telling you whether the code is correct or not? If it is, go find an ACTUAL editor and not some goofy "tool"

    You might also want to look at do/while instead of while{}. It may better fit this task. Also, if it's supposed to be BETWEEN 0 and 10, shouldn't it be guessing 1..9?
     
    deathshadow, Nov 23, 2019 IP
  3. mmerlinn

    mmerlinn Prominent Member

    Messages:
    3,197
    Likes Received:
    818
    Best Answers:
    7
    Trophy Points:
    320
    #3
    You are asking your question in the WRONG forum. This forum is for JavaSCRIPT questions NOT Java questions. Likely FEW if ANY people here can answer your question.
     
    mmerlinn, Nov 23, 2019 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Looks like Javascript to me and I'm guessing the homework assignment wanted to see a while being used. I'd submit that, it looks fine.
     
    sarahk, Nov 23, 2019 IP