0

I have a JS code whitch I wrote to add the values of variables together, but it just writes the values next to each other, it does not add them together. How to I add these values together?

JS:

function display(){
 let dollarvalue = `$ ${motorverseny}+${motorprofi}+${motoralap}+${turboverseny}+${turboprofi}+${turboalap}+${ecuverseny}+${ecuprofi}+${ecualap}+${valtoverseny}+${valtoprofi}+${valtoalap}+${felfuggesztesverseny}+${felfuggesztesprofi}+${felfuggesztesalap}`;
 let ppvalue = `${motorvenom}+${turbovenom}+${ecuvenom}+${valtovenom}+${felfuggesztesvenom}`;
 document.getElementById("pptext").innerHTML = `${ppvalue}`;
 document.getElementById("dollartext").innerHTML = `${dollarvalue}`;
}

HTML:

<h3 class="dollar">Dollar:</h3>
<h5 id="dollartext" style="color: #7cc576;"></h5>
<h3 class="pp">PP:</h3>
<h5 id="pptext" style="color: blue;"></h5>
wazz
5,0885 gold badges22 silver badges36 bronze badges
asked Dec 29, 2021 at 12:35
1
  • No, you're not adding anything together here. You're literally creating a string which contains numbers and + signs. Commented Dec 29, 2021 at 12:38

2 Answers 2

4

This is because you're not actually summing the values, but you are using string interpolation incorrectly. You need to place the variables within the curly braces and sum them. Here is an example:

let var1 = 10;
let var2 = 20;
let dollarValueWrong = `$ ${var1}+${var2}`; // results in the string `$ 10+20`
let dollarValueCorrect = `$ ${var1+var2}`; // results in the string `$ 30`
answered Dec 29, 2021 at 12:41
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for clearing this for me.
0

How about removing the interpolation?

let ppvalue = motorvenom+turbovenom+...
answered Dec 29, 2021 at 12:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.