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()

14th Mar 2021, 5:19 PM
Nirvashtypezero
Nirvashtypezero - avatar
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
14th Mar 2021, 5:48 PM
visph
visph - avatar
+ 1
James E. Ronco didn't my answer solve your problem?
14th Mar 2021, 5:54 PM
visph
visph - avatar
0
unfortunately Daniel Briceño didnt solve it. but visph solution worked.
14th Mar 2021, 6:01 PM
Nirvashtypezero
Nirvashtypezero - avatar
0
why not marking my answer as best, so?
14th Mar 2021, 6:02 PM
visph
visph - avatar
0
visph cant, the app keeps giving me an error everytime i click it.
14th Mar 2021, 6:03 PM
Nirvashtypezero
Nirvashtypezero - avatar
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...
14th Mar 2021, 6:10 PM
visph
visph - avatar
- 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.
14th Mar 2021, 5:37 PM
Daniel Briceño
Daniel Briceño - avatar