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);