Arithmetic mean in Javascript

Asked

Viewed 478 times

2

I’m having trouble calculating the arithmetic average of two notes in my Javascript program. When I put 10 on Nota1 and 10 on nota2, I follow to make the media, but unfortunately the final result of (10 +10)/2 is resulting in 505. Below the code:

//Pedido das notas
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 ")
}

Please signal me where I’m going wrong. Thanks in advance

  • 2

    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

2 answers

5


About the prompt:

Please note that the result is a string. This means that you must sometimes convert the value given by the user. For example, if the answer should be a number, you should convert the value to Number: var anumber = Number(window.prompt("Type a number", ""));

If you do not convert, you are doing "10" + "10" = "1010" / 2 = 505

https://developer.mozilla.org/en-US/docs/Web/API/window/prompt

1

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

comparação

Browser other questions tagged

You are not signed in. Login or sign up in order to post.