Another Javascript triumph!

Today I made this! I am very proud and feel like my Javascript knowledge is coming together, thank you to all the people who’ve helped me on the forum.

This calculates the sum of three numbers, outputs the result then tells you if the number is negative, zero or positive. :smiley:

3 Likes

Huh. It does some weird things when I try to add numbers… :thinking:

Oh well I got multiplication and division working. Anyway, still proud of it.

Hi Ella,

Great job! The reason it’s giving you weird results is because your initial maths function isn’t returning anything. It is successfully logging the number you want to the console, but it is unable to pass this result to calculated because there is no return statement!

Also, the way your following code is written, “Your number is negative” will only be logged to the console is the value of calculated is equal to -1. I think maybe you meant to put

if (calculated <= -1)

instead of

if (calculated == -1)

Maybe?

1 Like

Ah thank you this is really helpful! Can’t believe I forgot about greater and less than. :sweat_smile:

I will edit my code to make it work better thanks for the help.

I’m having a little difficulty getting this to work. I know the return needs to go inside the function, but where do I now put the

console.log("Your number is " + one + two + three);

Part?

Any tips very welcome.

  1. save the sum to a variable
  2. reference that sum variable in the string that you log
  3. return that sum variable

(is one option)

1 Like

So looking at your Codepen as it is now (which I would recommend posting along with your replies), you have written return maths, which is not what you want to return. Because your function is designed to perform math operations on a group of numbers, the returned value should be the final value after those operations are performed!

So from there you have a few choices. As @colinthornton mentioned above, you could first save the math result to a variable, use that variable in your console.log, and then return that variable.

Or if you want to change as little as possible of your code as it is now, return one plus two plus three. However I would recommend the first option as it is cleaner and reduces repeated code!

1 Like

I tried to do that but it won’t return a number anymore.


//Declare the function
function maths(one, two, three){
  //change the sum and log in the console
  let sum = one + two + three;
  return sum;
}

//initialise the calculated variable. assign calculated to the function and call it with those arguments
let calculated = maths(1,2,2);

//if the number is negative log it as such
if (calculated <= -1){
  console.log("Your number is negative");
}
//if the number is 0 log it as such
else if (calculated == 0){
console.log("Your number is 0");
}
//if the number is anything else it will be positive so log it as such
else{
console.log("Your number is positive");
}

Looks like you deleted the console.log from your maths function. From your above code, don’t change anything else, just put the console.log back in between your sum declaration and your return statement and it should work just as you want!

The function still doesn’t add the numbers together. My result is ‘122’, not the sum of adding those numbers.


//Declare the function
function maths(one, two, three){
  //change the sum and log in the console
  let sum = one + two + three;
  console.log("Your number is " + one + two + three);
  return sum;
}

//initialise the calculated variable. assign calculated to the function and call it with those arguments
let calculated = maths(1,2,2);

//if the number is negative log it as such
if (calculated <= -1){
  console.log("Your number is negative");
}
//if the number is 0 log it as such
else if (calculated == 0){
console.log("Your number is 0");
}
//if the number is anything else it will be positive so log it as such
else{
console.log("Your number is positive");
}

In your console.log, you can use your newly created sum variable instead of typing out one + two + three!

2 Likes

Ah thank you!
Blimey can’t believe I forgot I could do that with my code.
Thank you so much for the help! :smiley: