Printing the media [closed]

-1

I'm trying to do an average calculation between 2 values, but at the time of printing it's printing wrong, what am I missing?

var p1 = Number ('7.0')
var p2 = Number ('9.3')

const media = 2 / (p1 + p2)


if (media > 5.9) {
    console.log("Aprovado")
} else {
    console.log("Reprovado")
};
    
asked by anonymous 11.10.2018 / 02:00

4 answers

2

The correct is to add first and divide and not the other way around:

const media = (p1 + p2) / 2

Otherwise the result would be 2 divided by 16.3, which gives 0.12.

See the example:

var p1 = Number ('7.0')
var p2 = Number ('9.3')

const media =  (p1 + p2) / 2


if (media > 5.9) {
    console.log("Aprovado")
} else {
    console.log("Reprovado")
};
    
11.10.2018 / 02:02
1

It is printing right because 2 divided by 7 + 9.3 will always give mean less than 5.9.

See your code running

var p1 = Number ('7.0')
var p2 = Number ('9.3')

const media = 2 / (p1 + p2)

console.log(media);


if (media > 5.9) {
    console.log("Aprovado")
} else {
    console.log("Reprovado")
};

Correct is the reverse of this division, see

var p1 = Number ('7.0')
var p2 = Number ('9.3')

const media = 1/(2 / (p1 + p2))

console.log(media);


if (media > 5.9) {
    console.log("Aprovado")
} else {
    console.log("Reprovado")
};

Complicated?

Because the denominator has a fraction, then we use that familiar rule:

repeats the first and multiplies by the inverse of the second, that is, repeats the numerator and multiplies by the inverse of the denominator.

     p1+p2
1 x _______ = (p1+p2)/2
       2
  

They will not say that my answer is the same as other non-heim: D

    
11.10.2018 / 03:39
0

It seems to me a matter of math. Add first and divide later. I suggest using the parseFloat JavaScript function for the values n1 and n2 instead of Number

var p1 = parseFloat(7.0)
var p2 = parseFloat(9.3)

const media = (p1 + p2) / 2


if (media > 5.9) {
  console.log("Aprovado")
} else {
  console.log("Reprovado")
};
    
11.10.2018 / 02:46
0

The code was done correctly, only the divisor order was wrong. When you put the division with (2 / number of the sum) the divisor is the number of the sum (greater than the number to be divided) in the case should be the opposite 0 (number of the sum / 2), then 2 would be the divisor. Dai yes it is possible otherwise it will always be zero or those broken in case of real number as used.

    
11.10.2018 / 03:40