Return Undefined

Asked

Viewed 106 times

1

The code itself is working, as far as I’m concerned, the problem is that I’m not sure why my function is returning Undefined. It’s a simple function that’s returning what I really want, but I really don’t understand this Undefined.

Follows the code:

var nomeAluno = ["Gabriel", "Joao", "Maria", "Joaquim", "Joana"];

var np1 = [10, 9.2, 7, 0, 10];

var np2 = [7, 10, 9, 3, 9.8];

function calculaMedia(nota1, nota2){
    return (nota1 + nota2) / 2;
}

function condicao(media){
    if(media >= 7){
        console.log("Aluno aprovado!");
    }else{
        console.log("Aluno reprovado");
    }
}

for (i in nomeAluno){
    media = calculaMedia(np1[i], np2[i]);
    console.log("Aluno, " + nomeAluno[i] + ", NP1: " + np1[i] + ", NP2: " + np2[i]
+ " Média: " + media + ", Condição: " + condicao(media) +"\n");
}

What he returns to me: resultado

  • console.log inside a console.log?

1 answer

4


The function is not returning anything, it is printing something on the console, but why print something if you want this function to result in something to print? Then change:

var nomeAluno = ["Gabriel", "Joao", "Maria", "Joaquim", "Joana"];
var np1 = [10, 9.2, 7, 0, 10];
var np2 = [7, 10, 9, 3, 9.8];

function calculaMedia(nota1, nota2){
    return (nota1 + nota2) / 2;
}

function condicao(media){
    return media >= 7 ? "Aluno aprovado!" : "Aluno reprovado";
}

for (let i in nomeAluno) {
    let media = calculaMedia(np1[i], np2[i]);
    console.log("Aluno, " + nomeAluno[i] + ", NP1: " + np1[i] + ", NP2: " + np2[i] + " Média: " + media + ", Condição: " + condicao(media) +"\n");
}

I put in the Github for future reference.

I took the opportunity to improve some things, but I didn’t improve everything.

Browser other questions tagged

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