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

Hi there, I am stuck in the exercise for Loops, If-else & Boolean’s.

The solution in the PDF is

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

But when I paste that into the console in Chrome it states the error message

Uncaught SyntaxError: Identifier 'counter' has already been declared
    at <anonymous>:1:1

does anybody know what is wrong here? I am stuck at this point since the solution from the academy doesn’t work.

1 Like

It works for me but i am using Atom to run the console log not the console in Chrome. Its a package you can download in Atom it’s really useful i would download it. btw try this method i seem to like it better and there are a lot of mistakes in lots of the solutions moving forward i found that they are minor mistakes but can be confusing just google it. Try using this code

var str = “#”;
while (str.length <= 7) {
console.log(str);
str += “#”;
}

Thanks dude! I didn’t find that package, is it directly inside of Atom?

https://youtu.be/_k_pTbHB04A

Awesome, thank you Daniel!
After installing node.js additionally, prompt() commands still don’t work. Can they only be done in a browser maybe?

you need to create a new folder and name it with a .js like TestSite.js and then load this in your source file on html like this <!script src=“TestSite.js”>. (remove the “!” for it to run properly it doesnt show when i write exact code on here)

do your work in this file and hit ctrl+shift+b to run it. After you save the code and folder then open your web page and prompt() should work just make sure you save. and this preview window will help.https://youtu.be/Jr7eqR3NN5U

Remember this package only shows whats in the console.log other things such as prompt() and alert() only show when you load a web page so make sure you save your code then load the page and the alert will work… btw i am new too and could be wrong but hell it’s a learning process for all of us.

1 Like

1 Like

1 Like

Alright, got it running so far. You helped me a lot - Thank you very much :slight_smile:

1 Like

Hi Guys!
The text below is the solution that was provided to me for Javascript Ex 18. Unfortunately it didn’t produce any results (I’m using Atom). For the “Else” part, I don’t know what to tell the computer because the answer is basically anything else besides “Toronto”. But I don’t know how to communicate that. The consul keeps saying the affirmative answer “That’s correct!” regardless of the answer I put, even if it’s the wrong answer, such as “Boston”. Any suggestions? Thanks.

var answer = prompt( “What is the capital of Morocco?” );
var correct_answer = “Rabat” ;
if (answer == correct_answer) {
console .log( “Exactly” );
}
else {
console .log( “Sorry, the answer was” , correct_answer);
}

Hey there @JCrypto!
I am very new to JS by myself but I think I may have the solution for your problem.
“if (CorrectAnswer)” checks if the variable you called “CorrectAnswer” is true - Since every not-empty string is true, you will always get the “That’s correct!” alert.

Code which worked for me:

      var triviaquestion = prompt("What is the largest city in Canada?");
      var CorrectAnswer = "Toronto";

      if (triviaquestion === CorrectAnswer) {
        alert ("That is correct!");
      }
      else {
        alert ("Sorry, wrong answer!");
      }

Thanks! Much appreciated.

1 Like

Hi, just would like some one to explain.
var a = 10;
var b = 15;
if (a < b) {
console.log(“a is greater than b”);
}
else {
console.log(“a is not greater than b”);
}

This solution does not look right to me.
If you run it in the console as is then the “a is greater then b” message comes up.

If i change if (a < b) {
to
if (a > b) {
then the right message comes up.
Is there an error in the solution or is there something i am not seeing?

If the value you put in for “a” in javascript is more than the value for “b” then the computer should act accordingly. I was having a bit of trouble with that as well. Did you do the one that asks the user to input “greater or lesser than ten?” I don’t know if you have to input individual numbers or if the consul should understand the symbol for “less than”.

I suspect it was a mistake on their part, it should be “a is greater than b”

Hi guys, I’m having trouble with Question 24: the bus.

I have NO idea how to work out the formula for “if the bus has more than 30 passengers how many people have to walk”.

Does anyone have ideas how to calculate that part of it?
(I tried to Google it but I don’t actually know WHAT to Google, you know, what is this function/formula called, that I’m looking for)

Here is what I’ve got so far, but I need that “formula” to work out X (how to calculate the bus and the excess passengers, to work out how many people must walk). I’m looking on w3schools.com but I’m not sure what the “thing” is called that I’m looking for, if that makes sense.

Hey @Marcelle, hope you are good.

the exercise is quite simple, just need to use a conditional.

Here is my solution:

var text = prompt("how many passengers?")
var station1 = parseInt(text)
if(station1 > 30){
  var walk = station1 - 30
  console.log("too many passengers, " + walk + " must walk")
}else{
  console.log("lets ride!")
}

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

Carlos Z.

Hey Jott,
got a (dumb) question, maybe you can help me?
when you type the first line , the prompt function immediately prompts the question , but the if and else functions aren’t yet formulated… because you have to press enter to start a new line for the if and else…
Do you know what I mean and how to fix it that you can write the whole code before it gets prompted?

Hey Kim! Of course, I’ll try :slight_smile:

Are you referring to this post: Stuck in "Practice exercise - Loops, If-else & Boolean's" ?

Hey Jott, I almost figured it out instantly after I asked the question… Really simple solution… So nevermind and good luck with the course…