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

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…

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