When we insert the numbers 10 and 10 into text fields the values returned for each field are also text ("string").
There are other methods (besides Number) to get around this question.
Function parseint() - This Javascript function parses the "string" argument and returns an integer numeric value in the specified base.
Function parseFloat() - parses the "string" argument and returns a floating point number, ie returns decimal values.
Beyond this functions we can in other ways.
1- unary operator "+", signalling +
before the prompt
var nota1 = +prompt(" Informe a primeira nota do aluno: ")
var nota2 = +prompt(" Informe a segunda nota: ")
//Calculo da media
var media = ((nota1 + nota2) / 2)
document.write("a media do aluno é: ", media, "<br>", "<br>")
//Pedido da frequencia
var frequencia = prompt(" informe a frequencia do aluno: ")
//Verifica se está aprovado
if (media >= 7 && frequencia >= 75){
document.write(" o aluno está aprovado ")
}
else {
document.write("<br>" , " o aluno está reprovado ")
}
2 - multiplying the prompt by 1
var nota1 = prompt(" Informe a primeira nota do aluno: ")*1;
var nota2 = prompt(" Informe a segunda nota: ")*1;
//Calculo da media
var media = ((nota1 + nota2) / 2)
document.write("a media do aluno é: ", media, "<br>", "<br>")
//Pedido da frequencia
var frequencia = prompt(" informe a frequencia do aluno: ")
//Verifica se está aprovado
if (media >= 7 && frequencia >= 75){
document.write(" o aluno está aprovado ")
}
else {
document.write("<br>" , " o aluno está reprovado ")
}
3- Finally, any Thematic operation converts them into numbers, for example ...
console.log(typeof("10" / 1));
console.log(typeof("10" * 1));
console.log(typeof("10" - 1 + 1));
console.log(typeof("10" - 0));
console.log(typeof(Math.floor("10")));
//Se você deseja converter apenas para números inteiros, um modo rápido (e curto) é o double-bitwise not (isto é, usando dois caracteres de til)
console.log(typeof(~~"10"));
console.log(typeof("10" | 0));
Fastest "10"*1;
Speed Comparison (Mac Os only)
For Chrome 'plus' and 'Mul' are faster (> 700,000.00 op/sec), 'Math.floor' is slower. For Firefox 'plus' is slower (!) 'Mul' is the fastest (> 900,000,000 op/sec). In Safari 'parseint' is fasting, 'number' is slower (but the results are quite similar, > 13,000,000 <31,000,000). Thus, Safari for string to int is more than 10 times slower than other browsers. So the winner is mul
The prompt is returning "10" as text and not as number. There is "1010" /2 which by JS is 505.... Use Number(prompt(....)) which solves
– LLeon