Hello all I am trying to complete the second task apart of the MDN exercise quoted above. This is however proving a bit more complicated than anticipated, the problem I am having is that whenever I submit my code in the MDN playground. I am getting a response such as
“Your score is 50
“That was a terrible score — total fail!”
This should not be as I intend for it to show the response matching the score range based on the task information such as
“Your score is 50
“You did a passable job, not bad!”;
Additionally regardless of the value I replace the 75 in the score variable, I still receive “That was a terrible score — total fail!” instead of the appropriate response for the score value matching the parameter for the else if conditionals. Below is my code
let response;
let score = 60;
let machineActive = true;
if (machineActive) {
response = "You have to turn on the score machine";
if (score < 0 || score > 100) {
response = "This is not possible, an error has occurred.";
} else if (score >= 0 || score <= 19) {
response = "That was a terrible score — total fail!";
} else if (score >= 20 || score <= 39) {
response = "You know some things, but it's a pretty bad score. Needs improvement";
} else if (score >= 40 || score <= 69) {
response = "You did a passable job, not bad!";
} else if (score >= 70 || score <= 89) {
response = "That's a great score, you really know your stuff.";
} else if (score >= 90 || score <= 100) {
response = "What an amazing score! Did you cheat? Are you for real?";
}
}
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = `Your score is ${score}`;
para2.textContent = response;
section.appendChild(para1);
section.appendChild(para2);
Interestingly I do get the appropriate response when for the first score conditions being lesser than 0 or greater than 100 resulting in “This is not possible, an error has occurred.” I don’t want the answer I wish to know though where the error might be.
Noted I have and noticed that was apart of the problem, I guess what I did not get is that the console kept returning the second response for the second conditional for the scores due to me using the score >= 0 for the first operand for the second conditional which would imply that as long as the score is 0 as well as any number beyond 0 then it would keep defaulting to that second response given the previous conditional would be clearly explicit and the error would have started from the second. Thanks again for the advise.
let response;
let score = 90;
let machineActive = false;
if (machineActive === false) {
response = "You have to turn on the score machine to see response for your score";
} else {
if (score < 0 || score > 100) {
response = "This is not possible, an error has occurred.";
} else if (score === 0 || score <= 19) {
response = "That was a terrible score — total fail!";
} else if (score === 20 || score <= 39) {
response = "You know some things, but it's a pretty bad score. Needs improvement";
} else if (score === 40 || score <= 69) {
response = "You did a passable job, not bad!";
} else if (score === 70 || score <= 89) {
response = "That's a great score, you really know your stuff.";
} else if (score === 90 || score <= 100) {
response = "What an amazing score! Did you cheat? Are you for real?";
}
}
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = `Your score is ${score}`;
para2.textContent = response;
section.appendChild(para1);
section.appendChild(para2);
That was the problem , thanks for pointing that out. It worked by me changing the >= to === for strict comparison. I also had to change the first operand for each following condition by using same. There was an issue with the first part too with the response not being shown if the machine was off but it also involved the same issue.
let response;
let score = 90;
let machineActive = false;
if (machineActive === false) {
response = "You have to turn on the score machine to see response for your score";
} else {
if (score < 0 || score > 100) {
response = "This is not possible, an error has occurred.";
} else if (score === 0 || score <= 19) {
response = "That was a terrible score — total fail!";
} else if (score === 20 || score <= 39) {
response = "You know some things, but it's a pretty bad score. Needs improvement";
} else if (score === 40 || score <= 69) {
response = "You did a passable job, not bad!";
} else if (score === 70 || score <= 89) {
response = "That's a great score, you really know your stuff.";
} else if (score === 90 || score <= 100) {
response = "What an amazing score! Did you cheat? Are you for real?";
}
}
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = `Your score is ${score}`;
para2.textContent = response;
section.appendChild(para1);
section.appendChild(para2);