Help with lesson 9 #6

on the 2nd and final step…
Print: [“the sum of the numbers are:”] and the [sum] of variable [a] and [b] to the console.

here is my original and i believe my best attempt after spending an hour trying to get it to print the sum.

console.log(“the sum of the variables are:” + a+b );
VM5649:1 the sum of the variables are:45

Am i looking at it wrong… just realized maybe this is the accepted answer and not 9 (4+5) what i was looking for. Here is the lesson in more detail:

  1. Summary of integers.

  2. Create two integer variables and call them a and b . Save the values 4 and 5 in the variables.

  3. Print “ The sum of the numbers are: ” and the sum of variable a and b to the console.

1 Like

There are at least two ways to solve this:

let a = 4; 
let b = 5;
console.log("The sum is: " + (a+b));

Note that with this solution, you will have to put the calculation of a + b into brackets (a + b), otherwise JavaScript will concenate a and b as a string to “The sum is”. and return “The sum is 45.”

let a = 4;
let b = 5;
let sum = a + b;
console.log("The sum is: " + sum);

JSLesson9#6
Note: I freaking love GIFs!

4 Likes

The students will love these small tips that you keep giving out! Keep up the Awesome work! :smiley:

2 Likes

Thank you for your great response! Today after coming back to it and re-watching a bit of ivan talk in the previous video and with your text above I noticed immediately what i had done. “4” “5”. quotations around my numbers. :man_facepalming: I thought I had checked for that previously but anyway I got it working now!!

2 Likes

@Malik @Bhujanga
Hey you two… When looking at the solutions to this lesson… number 9 and 10 ( the last two) I notice that the text prompt is used. Why? when I type prompt just like the solutions mine give a lengthy error msg.

1 Like

can you share your code with us?

  1. Create a script that requests and accepts two integers.
  2. The script should then calculate the product of the two numbers and print the answer to the console.

var x = 19;
var y = 22;
console.log(The sum of x and y equal: " + (x+y));
The sum of x and y equal: 41

  1. Console.log user input.

  2. In this exercise, you will write a script that receives a message from the user and print it out to the console.

console.log(“Hello friends, I have some great news today!”);
Hello friends, I have some great news today!

the exercise says: “requests two integers”
I figure this means that the script shall ask the user for numbers. Therefore you would use var a = prompt("Insert your first number"); var b = prompt("Insert your second number"); console.log("The sum of" + a + " plus " + b + " equals to: " + parseInt(a) + parseInt(b))."

I see why it wasn’t working now… Because I was on Microsoft edge, because that’s where the solutions file download opens the prompts do not work for some reason. Why would this be happening? Same code works fine on my regular browser i use… chrome.

When it says PRODUCT does that infer multiplication?

Yes.

There are different implementations for both the browsers and some features differ in execution. Most likely than not, developers focus on chrome as it is the defecto standard for building web applications. So you should be good in that regard.

1 Like