skip to Main Content

The main purpose of this application is i need an overall score once the user selects multiple selections all the functionality is working however once i click the generate overall score button i want to see the overall score. But i can’t i just see the whole array with each individual value.

const scoreValue1 = `The Score Is: ${ theScores.scores0}`;

const appendedValue = $("#score-1").append(scoreValue1);
console.log(appendedValue);

totalScores.push(scoreValue1)

const scoreValue2C = `The Score Is: ${ theScores.scores2}`;
const appendedValue = $("#score-2").append(scoreValue2C);
console.log(appendedValue);

totalScores.push(scoreValue2C)

const totalScores = []

Screenshots:
Overall Score Result Total Score Array
Appended Score
Appended Score

2

Answers


  1. If i correctly understood, you have an array which contains integers.
    And you want to sum all of these numbers ?

    If yes, you can use ".reduce()" function.

    //Imagine, your result array is like this
    const totalScore = [1, 2, 3]
    
    const sum = totalScore.reduce((acc, val) => {
      return acc + val;
    }, 0);
    
    console.log(sum); // output is 6
    
    Login or Signup to reply.
  2. Have you checked the console for any error? The keyword ‘const’ in JavaScript works a little bit differently than let and var. Any variables declared with var will be hoisted. This means that you can access the variable without the JavaScript engine throws an error. However, the value of that variable will be undefined.
    Any variables declared with the let keyword are not hoisted. This means that when you try to access the variable created with var, it will throw an error.
    Const works the same way but you cannot change the value. In this case, you are trying to push an array element without declaring a first. Therefore, the JavaScript engine will throw a reference error. A good practice is always declare your variable with let or const first. This will not only prevent the polution of the global object )window for browser, global for nodejs), but it also reduces the amount of errors. Since you did not provide enough debugging details, there is nothing I can do for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search