Need help with getElementByID in Project

Hi Everyone!

I’m new to Javascript and taking a course. I completed my assignment and hope it’s correct.
I would GREATLY appreciate you help! :slight_smile:

My assignment is the following:

  1. 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.";
}

There are a number of issues there, so where to begin? From the experience and output of the code.

When you run this code there are two prompts for a name. There should only be one prompt for a name.

The score also needs to be outside of quotes, in the same way that is done with the response variable.

3 Likes

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 :wink: 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.

2 Likes

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.

Greatly Appreciated! :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.