Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Upon submitting the "Name" of the user after finishing the quiz I need the alert message to show his/her numerical standing based on their quiz score with the name they submitted. One of the minor issues I am encountering is that the alert reads back to me 


[object HTMLInputElement] Where the "Name" of the user should be. 



JavaScript
<pre>const startButton = document.getElementById('start-btn');
const nextButton = document.getElementById('next-btn');
const scoreButton = document.getElementById('score-btn');
const questionContainerElement = document.getElementById('question-container');
const questionElement = document.getElementById('question');
const answerButtonsElement = document.getElementById('answer-buttons');
const scoreTracker = document.getElementById('score-tracker');
const scoreUpElement = document.getElementById('score-up');
var firstName = document.getElementById('firstname');
var submitForm = document.getElementById('submit-form');

let randomQuestions, currentQuestionIndex;

startButton.addEventListener('click', startGame);
nextButton.addEventListener('click', () => {
  currentQuestionIndex++
  setNextQuestion()
});
/*scoreButton.addEventListener('click', scoreList);*/

function startGame() {
  startButton.classList.add('hide');
  randomQuestions = questions.sort(() => Math.random() - .5);
  currentQuestionIndex = 0;
  questionContainerElement.classList.remove('hide');
  scoreTracker.classList.remove('hide');
  setNextQuestion();
  scoreUpElement.textContent = 0;
  scoreButton.classList.add('hide');
};

function setNextQuestion() {
  resetState();
  showQuestion(randomQuestions[currentQuestionIndex]);
};

function showQuestion(question) {
  questionElement.innerText = question.question;
  question.answers.forEach(answer => {
    const button = document.createElement('button');
    button.innerText = answer.text;
    button.classList.add('btn');
    if (answer.correct) {
      button.dataset.correct = answer.correct;
    }
    button.addEventListener('click', selectAnswer);
    answerButtonsElement.appendChild(button)
  })
};

function resetState() {
  clearStatusClass(document.body);
  nextButton.classList.add('hide');
  while (answerButtonsElement.firstChild) {
    answerButtonsElement.removeChild(answerButtonsElement.firstChild)
  }
};

/* Checks if selected button is part of the correct dataset */
function selectAnswer(e) {
  const selectedButton = e.target;
  const correct = selectedButton.dataset.correct;
  
  processResults(correct);
  setStatusClass(document.body, correct);

  Array.from(answerButtonsElement.children).forEach(button => {
    setStatusClass(button, button.dataset.correct)
  })

  if (randomQuestions.length > currentQuestionIndex + 1) {
    nextButton.classList.remove('hide');
  } else {
    startButton.innerText = 'Restart';
    startButton.classList.remove('hide');
    scoreButton.innerText;
    scoreButton.classList.remove('hide');
  }
};

function setStatusClass(element, correct) {
  clearStatusClass(element);
  if (correct) {
    element.classList.add('correct');
  } else {
    element.classList.add('wrong');
  }
};

function clearStatusClass(element) {
  element.classList.remove('correct');
  element.classList.remove('wrong');
};


/* Game questions with 4 total choices */
let questions = [{
    question: 'What year did the Raiders win their last Super Bowl?',
    answers: [{
        text: '2003',
        correct: true
      },
      {
        text: '1993',
        correct: false
      },
      {
        text: '1983',
        correct: false
      },
      {
        text: '1972',
        correct: false
      },
    ]
  },
  {
    question: "What was Jack Tatum's nickname?",
    answers: [{
        text: 'Killer Croc',
        correct: false
      },
      {
        text: 'The Assassin',
        correct: true
      },
      {
        text: 'The Jackhammer',
        correct: false
      },
      {
        text: 'Killer Bee',
        correct: false
      },
    ]
  },
  {
    question: "Which 'Hall of Fame' Raider wore the number 00",
    answers: [{
        text: 'Charles Woodson',
        correct: false
      },
      {
        text: 'Kenny Stabler',
        correct: false
      },
      {
        text: 'Mike Haynes',
        correct: false
      },
      {
        text: 'Jim Otto',
        correct: true
      },

    ]
  },
  {
    question: 'Which Raider became a HollyWood star?',
    answers: [{
        text: 'Carl Weathers',
        correct: true
      },
      {
        text: 'Howie Long',
        correct: false
      },
      {
        text: 'Lyle Alzado',
        correct: false
      },
      {
        text: 'Bo Jackson',
        correct: false
      },
    ]
  },
  {
    question: 'Who is Al Davis?',
    answers: [{
        text: 'Current star Wie Receiver',
        correct: false
      },
      {
        text: 'Current Owner',
        correct: false
      },
      {
        text: "QB from the late 70's",
        correct: false
      },
      {
        text: 'Ex Owner',
        correct: true
      },
    ]
  },
];


function processResults(isCorrect) {
  if (!isCorrect) {
    return;
  }
  
  const scoreUp = parseInt(scoreUpElement.textContent, 10) || 0;

  scoreUpElement.textContent = scoreUp + 100;
};

/*submitForm.addEventListener('click', formSubmission)*/

var scoreNames = [];

function formSubmission() {
  firstName.innerText
  if (confirm('Do you want to submit')) {
    alert(`Congrats ${firstName} you won!`)
  } else {
    return false;
  };


};




/*function formSubmission() {
  if (confirm('Do you want to submit')) {
    document.getElementById('submit-form');
  } else {
    return false;
  };


};*/

/*function store() {
  localStorage.setItem('name', name.value);
}*/
HTML
<pre><!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="assets/css/scores.css" />
    <script defer src="assets/js/script.js"></script>
    <title>High Scores</title>
  </head>

  <body>
    <div id="container-username">
      <div id="container-title">
        <h3>Type your name to see your score!</h3>
        <div>
            <form onsubmit="return formSubmission()">
            <input type='text' id='firstname' name='firstname' required />
            <input type='submit' id="submit-form" value="Go Now!">
            </form>
        </div>
    </div>
  </body>
</html>
CSS
<pre><!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="assets/css/scores.css" />
    <script defer src="assets/js/script.js"></script>
    <title>High Scores</title>
  </head>

  <pre lang="CSS">* {
    box-sizing: border-box;
    font-family:'Times New Roman', Times, serif;
}

:root{
    --hue-neutral: 213;
}

body {
    --hue: var(--hue-neutral);
    padding: 0;
    margin: 0;
    display: flex;
    width: 100vw;
    height: 100vh;
    justify-content: center;
    align-items: center;
    background-color: hsl(var(--hue), 31%, 19%);
} 

#container-username {
    width: 800px;
    max-width: 80%;
    background-color: #63768d;
    border-radius: 5px;
    padding: 10px;
    box-shadow: 0 0 10px 2px;
}

#container-title {
    text-align: center;
}

#username {
    text-align: center;
}

.center-me {
    text-align: center;
}


What I have tried:

These are some of the functions I tried
JavaScript
/*function formSubmission() {
  if (confirm('Do you want to submit')) {
    document.getElementById('submit-form');
  } else {
    return false;
  };


};*/

/*function store() {
  localStorage.setItem('name', name.value);
}*/
Posted
Updated 13-Aug-21 4:06am

1 solution

Look at your output request:

function formSubmission() {
  firstName.innerText
  if (confirm('Do you want to submit')) {
    alert(`Congrats ${firstName} you won!`)
  } else {
    return false;
  };


Notice that you use "firstName.innerText" (although what it's doing in that position in your script - just hanging there - eludes me). Anyway, the "." implies it's an object with one or more potential values. If you ask to display it without qualifying which of those values you wish to use then you are asking it to display an object with multiple components.

You need to tell it which component you wish to display.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900