Help with Javascript = ( =

Asked

Viewed 39 times

0

It was supposed to average 5.4 and appear 48.8 (Recital 2: 4.0 7.5 8.0 6.4)

var entrada = require('readline-sync')

var linha = entrada.question().split(' ')
var segundoValor = entrada.question()

var N1 = parseFloat(linha[0]).toFixed(1)
var N2 = parseFloat(linha[1]).toFixed(1)
var N3 = parseFloat(linha[2]).toFixed(1)
var N4 = parseFloat(linha[3]).toFixed(1)

var media = ((N1 * 2) + (N2 * 3) + (N3 * 4) + (N4 * 1) / 10)

if(media >= 7){
    console.log('Media: ', media)
    console.log('Aluno aprovado.')
}

  • 3

    Wouldn’t it be because of parentheses? The way you’re doing, (N4 * 1) will be divided by 10 before being added with the other values, since division has priority over sum. Try to put the / 10 outside the parentheses.

  • 1

    You entered with 5 parameters but you are only considering 4. Would this be it? If not, what is the last parameter for? "6.4".

  • You have 5 numbers in the lobby but only use 4... you can explain this part better so you can answer too?

1 answer

2

the problem is as the friend above said even:

//var entrada = require('readline-sync')
var entrada = "2.0 4.0 7.5 8.0 6.4"

var linha = entrada.split(' ')
//var segundoValor = entrada.question()

var N1 = parseFloat(linha[0]).toFixed(1)
var N2 = parseFloat(linha[1]).toFixed(1)
var N3 = parseFloat(linha[2]).toFixed(1)
var N4 = parseFloat(linha[3]).toFixed(1)
var N5 = parseFloat(linha[4]).toFixed(1)

var media = ((N1 * 2) + (N2 * 3) + (N3 * 4) + (N4 * 1)) / 10

if(media >= 7){
    console.log('Media: ', media)
    console.log('Aluno aprovado.')
}
console.log('Media: ', media)

Browser other questions tagged

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