Pregunta

I am a javascript student, just started. I use Sublime Text 3 and I can't figure out why the division operator (/) is not working for me, when I try the same code in jsfiddle it works perfectly. It is a very very vey simple task.

something as dumb as below:

<script>
var a;
var b;
var c;
a=parseInt(prompt("Enter first number"));
b=parseInt(prompt("Enter second number"));
c=parseInt(prompt("Enter third number"));
var total=(a+b+c);
var avg=total/3;
alert(a +", "+ b +", "+ c + " | Average = "+avg);
</script>

Why is the division in this line var avg=total/3; not being recognised?? Is there a different way to divide?

Thank you in advance and happy new year!

Marcelo O.

¿Fue útil?

Solución

First, try checking the value and typeof the variable total to ensure it is an integer.

You should also add a radix to your parseInt functions, as this may be causing a, b, or c to be nAn see mdn for example

I would also recommend installing a static analysis tool for sublime, something like Sublime-JSHint You will find it extremely useful for catching syntax errors.

Otros consejos

Why do you say it is not running? If it works in your fiddle, which you say it does, it runs just fine. Sublime Text 3 is NOT a Javascript runtime environment.

You need to add more details as to what you mean when you say "not working".

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top