I’m new to Javascript and taking a course. I completed my assignment and hope it’s correct.
I would GREATLY appreciate you help!
My assignment is the following:
Create an if function that will display the result, with the following criteria:
a. If the user has completed their name in the prompt window, the paragraph with id “result” must display the message “Hello STUDENT! Your result is SCORE out of 10.”
i. Replace STUDENT with the name the user captured in the prompt window.
ii. Replace SCORE with the user’s score.
b. If the user has not completed their name in the prompt window, the paragraph with id “result” must display the message “Your result is SCORE out of 10.”
Replace SCORE with the user’s score.
Here is my Project Result.
I have an HTML file with the paragraph id attribute .
Therefore, I created one variable with outputx and another with outputy. One is for a student input and the other one is without a name.
From HTML File.
<p id="result"></p>
var response = prompt("What is your name?");
var outputx = document.getElementById("result");
var outputy = document.getElementById("result");
var score1 = 10;
var response = prompt("What is your name?");
if (response) {
outputx.innerHTML = "Hello" + response + "!. You have is +score1+ out of 10.";
}
if (response != null) {
outputy.innerHTML = "Your result is +score1+ out of 10.";
}
So as Paul has outlined, there are a couple of trip-ups you’ll want to correct, but on the whole the code feels ‘correct’ for a learning level student. In addition, a couple of pointers, not errors, but refinements and considerations for future growth:
The use of outputx and outputy are somewhat redundant; you could refer in both code blocks to outputx and be fine; the string you pass TO that element will be different depending on your logic, but you’re passing it to the same object regardless, so the extra reference is redundant.
I’m uncertain if your course has yet introduced you to the else clause of an if statement; if so, consider condensing your two ifs into a single one with an else clause. If the course hasn’t taught you that yet, ignore this point until it does What you’ve got at present is the literal interpretation of the assignment, which is good. It shows you’re able to interpolate instruction to code, which is the final step in the requirements->code process.
Wow! I accidentally copied the prompt… “What is your name” twice. I’m correcting this and the score information as well. You are a GREAT help… Thank YOU!
Oh yes! I see. I will follow your instructions. I LOVE this website and ALL the helpful information. I’m adding these helpful hints to my Javascript troubleshooting notebook.