0
I can't get the variables to add. They end up concatenating. Javascript.
function avg(){ let name = prompt('Enter Your Name') let test1 = prompt('Enter test score 1') let test2 = prompt('Enter test score 2') let test3 = prompt('Enter test score 3') // its concatenating instead of adding them to get the sum of the three test scores. I cannot figure out how to make it think their numbers. let avg = parseInt(test1) + parseInt(test2) + parseInt(test3) / 3; alert(`${name}, your averge score is ${avg}!`); } avg()
7 Respostas
+ 3
your solution doesn't concatenate scores as string, but you miss parenthesis to get the correct result:
let avg = (parseInt(test1) + parseInt(test2) + parseInt(test3)) / 3;
division has higher precedence than addition, so your solution first compute test3/3 and then add test1 and test2 ;P
+ 1
James E. Ronco didn't my answer solve your problem?
0
unfortunately Daniel Briceño didnt solve it. but visph solution worked.
0
why not marking my answer as best, so?
0
visph cant, the app keeps giving me an error everytime i click it.
0
wow, that was working on Daniel Briceño answer but not on mine?
I think there's a bug, and you should inform sololearn at info@sololearn.com...
- 2
And what if you concatenate them like this:
var total=name+", your averge score is"+avg+"!";
alert(total);
Variable concatenation is what I like the most about javascript. just do:
var string1="The numbers are: ";
var string2=" And so variables are concatenated";
string1+"1:"+1+" 2:"+2+""+string2;
This answer is not for this question.