Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript additions

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<h3 class="dollar">Dollar:</h3>
<h5 id="dollartext" style="color: #7cc576;"></h5>
<h3 class="pp">PP:</h3>
<h5 id="pptext" style="color: blue;"></h5>

>Solution :

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`
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading