Test your skills: Math2: I have a Question

Reference;
Test your skills: Math 2 exercise.

If I write the script as show in example 1 below, all works as should, as far as I can tell.

However, I’m somewhat confused, for example, if just for the sake of failure experimentation, I add the line “finalResult = ‘My Test For Failure, Text String’;” as shown in example 2 below, I will still get “The finalNumber is a number type. Well done!” text wrote to “para2”. Shouldn’t the text “Ooops! finalNumber is of type ${ typeof finalNumber }” be wrote to para2 in this senario?
Am I doing something wrong in example 1, which will always cause the well done string to be wrote to para2, even if the variable is not a number?

Example ONE: (CodePen)

let result = (7 + 13) / (9 + 7);
let result2 = 100 / (2 * 6);

result = result *= result2;
finalResult = result.toFixed(2);
finalNumber = Number(finalResult);

// Don't edit the code below here!

const section = document.querySelector('section');

let para1 = document.createElement('p');
para1.textContent = `Your finalResult is ${ finalResult }`;
let para2 = document.createElement('p');
let finalNumberCheck = typeof finalNumber === 'number' ? 'The finalNumber is a number type. Well done!' : `Ooops! finalNumber is of type ${ typeof finalNumber }.`
para2.textContent = finalNumberCheck;

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

Example TWO: (CodePen)

let result = (7 + 13) / (9 + 7);
let result2 = 100 / (2 * 6);

result = result *= result2;
finalResult = result.toFixed(2);
finalResult = 'My Test For Failure, Text String' ;
finalNumber = Number(finalResult);

// Don't edit the code below here!

const section = document.querySelector('section');

let para1 = document.createElement('p');
para1.textContent = `Your finalResult is ${ finalResult }`;
let para2 = document.createElement('p');
let finalNumberCheck = typeof finalNumber === 'number' ? 'The finalNumber is a number type. Well done!' : `Ooops! finalNumber is of type ${ typeof finalNumber }.`
para2.textContent = finalNumberCheck;

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

@DigitalEngine hello there!

you have just discovered a really weird quirk of JavaScript, which I didn’t know about either.

So it seems that if you take any variable and do Number(variable) on it, typeof variable will then return Number, even if it blatantly isn’t a number!

I read up on this, and it seems that a more foolproof way to test whether something is a number is to run isNaN(variable) on it, and see it it returns true or false.

I’ve updated the code in my examples to use isNaN(finalNumber) === false instead of typeof finalNumber === 'number' (I guess I could instead use !isNaN(finalNumber) to make it shorter?)

Yes, this works, or both works with my induced failure test.
I like the longer version sNaN(finalNumber) === false better I think, as it’s a little more intuitive of what its doing, for a rookie… :upside_down_face: such as myself anyway.
Again, thanks!
Ref: my test version on github Java: Test your Skills: Math Task 2