Stuck in "Practice exercise - Loops, If-else & Boolean's"

NEED HELP!
Thanks in advance!

Hi Everyone!

I’m currently working on this practice question.


28. While loop with input.
Exercise:

  1. Create a program that takes a number as input from the user, and prints all numbers from 0 up to this number (hint: while …).
  2. Expand the program with a new while loop that requests input from the user for each “round”.
  3. When the user enters the number 10, the program must end.

This is my solution so far, it loops just fine and requests for input after every round, however I can’t seem to figure out where to insert the statement that’ll allow the console to display “You entered 10, the program will end now!” when a user enters the number 10.

Will need someone more experienced than me to help out :smiley:


while(input != 10){

var input = prompt("Pick a 2-digit number.");     
var counter = 0;

    while (counter <= input){

        console.log(counter);
        counter++;     
    }

}


with thanks,
Chin

Hi Chin,

your first line is the error I think. The variable input is not defined at this point so the program goes on even though I enter 10 or more.

This is my solution:

var input = prompt("Pick a 2-digit number.");
var counter = 0;

    while (counter <= input){
        if (input >= 10) {
          console.log("You entered 10 or more. Program will end now.")
          break;
        }

        console.log(counter);
        counter++;
        }

After we enter the while loop, there’s an if-statement that checks if the input is equal or greater than 10. In that case it will print: “You entered 10 or more. Program will end now.” to the console and end the loop (before it has begun) with the break; statement.

Hope this helps :slight_smile:
Cheers,
Jan

3 Likes

Hi Jan,

Thanks for the super quick help! And yes this was definitely useful!

I have a better understanding of the positioning of variables and statements now.

Cheers!
Chin

2 Likes

hey @thecil - hope you are good :v:

can you explain to me : var station1 = parseInt(text)

…particulary what parseint does - I totally don’t understand it !

feeling pretty dumb doing this javascript course !

Thanks in advance,
Jonathon.

1 Like

the parseInt keyword is an internal function of javascript which can be use to transform a variable value to a integer value (if possible). Can also be use to validate that an input IS a integer valid value.

Here is a very simple code that can help you understand it a little bit more.

var text = "hello"
var numbers = 123
var float = 1.23
var example1 = parseInt(text)
var example2 = parseInt(numbers)
var example3 = parseInt(float)

console.log(example1, example2, example3) //should print "NaN 123 1"

Here are some links to learn more about it:

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

Hi,
Really struggling with the exercises. can anyone tell me why I’m getting this error for exercise 19? This is just for the initial part of the exercise.

var number= prompt(“Enter a number”);

if (number < 10);{console.log(“The number is below 10”);}
else {console.log(“The number is above 10”);}

Uncaught SyntaxError: Unexpected token ‘else’

1 Like

it seems the semi colon here was my error. Am i misremembering or was i not advised that when in doubt to include them as they are more likely necessary than unnecessary?

1 Like

Hi @Marklar, hope you are ok.

Now yeah the problem was the semi colon ;, you should keep in mind the basic syntax on the methods and operations on JavaScript.

the IF conditional basic syntax is always the same, which is:

if(condition){
 //do something...
}
else{
 //do this instead
}

Carlos Z

1 Like

Hey Carlos - I have another question - sorry this may become quite a regular thing ! :smiley:

i’m doing the exercise on functions - i’ve just copied exactly what Ivan has done in Atom and its not working… can you take a look below and explain to me why please, that way i’ll understand better i think (i get the main reason for using functions).

Thanks so much … the script i copied which isn’t working is as follows:

<script>

function greet(name){
document.write("<h1>greetings "+ name + "</h1>  how are you " + name + "hope you are doing well " + name);
}
greet(jonathon);

</script>

Hi @JDW,

The mistake over here is that you are passing a variable into the function call. As jonathon is not defined anywhere, the code does not work. The way to pass it is as a string – greet("jonathon");.

This will work.

Hope this helps :slight_smile:

1 Like

Thanks Thanks Thanks :pray:

1 Like

I have a question about the part in bold in the solution:

var stars = " "; var counter = 0;

while (counter < 9) {
stars = stars + “*”;

}

console.log(stars); counter++;

I don’t understand why stars = stars + “*”; returns an extra star each time the loop runs- where in the code is it instructed to add an additional star on each time the code loops? for me, i don’t see why it doesn’t just print one star each time as nothing here suggests an additional star will be added. any help would be greatly appreciated

is this all on the console ?

Question…

just working my way through every exercise and not looking at the answers til i figure it out, my question is on the stars exercise why do i need the += … why doesn’t just using + work ?

Sorry if its a dumb question - just need my brain to figure it out - thanks in advance :v:

var stars = " "
var counter = 0
while(counter<9){
console.log(stars += " * ");
counter++;
}

Hi @JDW,

stars += " * "

This basically means stars = stars + " * " Nothing fancy in its implementation. It’s just a shortcut way to write it.

Hope this helps.

Thanks buddy :+1: :v:

1 Like

Hi guys,

I’m also having some difficulties in running a script that needs to show the correct output. If I exactly copy the below code then it it just says: undefined

What am I doing wrong?

Can you screenshot the console with your code and the result?

I found out what I was doing wrong. I didn’t know how to jump to the next rule inside a script. So all the time I was pressing enter to execute the code instead of using SHIFT+ENTER to go to the next rule… I’m such a noob :wink:

1 Like