Basic math in JavaScript. Can you solve this?

I am learning Javascript Fundamentals, where I read an article at developer.mozilla.org.

Now at the end they have suggested 3 tests.

At test 1, I am stuck at " write a calculation that checks whether finalResult is an even number. Store the result in a variable called evenOddResult"

So, guys can you help with this?
All details at: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Math

And here is the whole question:

  1. Create four variables that contain numbers. Call the variables something sensible.
  2. Add the first two variables together and store the result in another variable.
  3. Subtract the fourth variable from the third and store the result in another variable.
  4. Multiply the results from the last two steps together, storing the result in a variable called finalResult . The product should be 48. If it isn’t, you’ll have to adjust some of the initial input values.
  5. Finally, write a calculation that checks whether finalResult is an even number. Store the result in a variable called evenOddResult .

let finalResult;

let evenOddResult;

// Add your code here
let a = 6;
let b = 6;
let c = 8;
let d = 12;

x = a + b;
y = d - c;
finalResult = y * x;

if
(finalresult%2===0)
{evenOddResult="even"}
else
{evenOddResult="odd}


// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);
    

Hi
In the test they say to add code
Can you show us what did u add so far? and what did u try?


let finalResult;

let evenOddResult;

// Add your code here



// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);

Hi @jatintheexplorer ! I think they want you to find whether the finalResult you get is an even or odd number.

To simply put even numbers are divisible by two while oddnumbers aren’t.

So we can say if(finalresult%2===0){evenOddResult="even"}else{ evenOddResult="odd}

Here, % is called the modulus operator which will give the remainder of the division operation.

1 Like

even numbers are divisible by 2

division by 0 is a discussion on its own, both mathematically and how languages deal with it

Yeah, so I did this and got a revert that :slight_smile:

Is the finalResult 48? Yes, well done!
The final result is odd. Hrm.

And here is the whole code:

let finalResult;

let evenOddResult;

// Add your code here
let a = 6;
let b = 6;
let c = 8;
let d = 12;

x = a + b;
y = d - c;
finalResult = y * x;


// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Hi,
So I added your suggested code, and still I am not getting:

SyntaxError: Invalid or unexpected token

Here is the full code I entered in Math test 1:


let finalResult;

let evenOddResult;

// Add your code here
let a = 6;
let b = 6;
let c = 8;
let d = 12;

x = a + b;
y = d - c;
finalResult = y * x;

if
(finalresult%2===0)
{evenOddResult="even"}
else
{evenOddResult="odd}


// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);
    

Test page: Test your skills: Math - Learn web development | MDN

It looks like you are missing a " after "odd.

Hi Jeremy LT, actually this is the 1st time I have ever worked on JS.
So I didn’t got what you suggested.

It looks like you are missing a " after "odd .

If you look at the syntax highlighting, everything goes wonky after "odd


You do not have a closing " on the string "odd".

1 Like

Oh my bad.

I actually corrected in in the developer mozilla test. So below the what I have written in the test result but still, it’s not right :frowning:


let finalResult;

let evenOddResult;

// Add your code here
let a = 6;
let b = 6;
let c = 8;
let d = 12;

x = a + b;
y = d - c;
finalResult = y * x;

if
(finalresult % 2 === 0)
{evenOddResult="even"}
else
{evenOddResult="odd"}


// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);
    

When I use repl.it, I can see an error with this line (small capitalization typo)

In this repl.it link, I added 'use strict' to help highlight those sorts of issues. There are 3 errors in strict mode, two undefined variables and one typo:
https://repl.it/repls/GlumStrikingConstants#index.js

it seems the evenOddResult variable should have a value of 0 or 1, not of string

2 Likes

change finalresult to finalResult in if condition.R is capital letter

Did that, but no luck…

Did you fix what @ilenia pointed out?

Trying this & its taking time as I am not sure how to format it…
How to write it exactly with the code :sweat_smile:

Learning a new language always takes a bit of time. Post more code and ask more questions if you get stuck!

1 Like

Ok, so should I mention it like this?


let finalResult;

let evenOddResult;

// Add your code here

let a = 6;
let b = 6;
let c = 8;
let d = 12;

x = a + b;
y = d - c;
finalResult = y * x;


if (finalResult % 2 === 0) {
  evenOddResult='The final result is even!'
} else {
  evenOddResult='The final result is odd. Hrm.'
}

// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
let finalResultCheck = finalResult === 48 ? `Yes, well done!` : `No, it is ${ finalResult }`;
para1.textContent = `Is the finalResult 48? ${ finalResultCheck }`;
let para2 = document.createElement('p');
let evenOddResultCheck = evenOddResult === 0 ? 'The final result is even!' : 'The final result is odd. Hrm.'
para2.textContent = evenOddResultCheck;

section.appendChild(para1);
section.appendChild(para2);
    

This line is already handling the output text for you for. It tests your output to see if it matches 0. It looks like you want set evenOddResult to either 0 or 1.

1 Like