Results of an inconsistent average calculation

Asked

Viewed 34 times

-1

In one exercise I asked to calculate the average of the notes,however at the time of showing appears a value completely out of logic

Script

   function cadastrarUsuario() {


        let nome = document.querySelector("#nome").value;
        let nota1 = document.querySelector("#nota1").value;
        let nota2 = document.querySelector("#nota2").value;
        let nota3 = document.querySelector("#nota3").value;
        getAprovado(nota1, nota2, nota3);
        criarUsuarioTable(nome,nota1,nota2,nota3)

    }

    function getAprovado(nota1, nota2, nota3) {

        console.log('numero um ',nota1)
        console.log('numero dois ',nota2)
        console.log('numero tres ',nota3)
        let media = (nota1 + nota2 + nota3) / 3;


        if (media >= 7) {
            alert('Aprovado')
        } else {
            alert('Reprovado')
        }
    }

    function criarUsuarioTable(nome,nota1,nota2,nota3){

       let html =  document.querySelector("#tabela");

       html.innerHTML =
       `
       <tr>
            <td>${nome}</td>
            <td>${nota1}</td>  
            <td>${nota2}</td>  
            <td>${nota3}</td>  
            <td>${(nota1+nota2+nota3)/3}</td>    

       </tr>

       `
    }

</script>

Console

Felipe Machado Da Silva 5 9 10 1970

  • Both the way inside the media string template, and the variable are giving the value "crazy"

2 answers

1

All data coming from a form comes as a text format, or conversion is required, parseint

        nota1 = parseInt(nota1);
        nota2 = parseInt(nota2);
        nota3 = parseInt(nota3);

-1

Try to display the variavel 'media' instead of repeating the calculation expression in tabela

Browser other questions tagged

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