Averaging

Asked

Viewed 60 times

-1

I am trying to make an average calculation between 2 values, but at the time of printing it is printing wrong, in what I am 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")
};
  • What’s wrong? How should it be? How is it coming out?

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 is 0.12.

Take 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")
};

  • but pq the order changes the result? in an equation are not the things in all parentheses that are solved first? and why the 2 / standing in front he prints failed? Thanks since ja <3

  • But you want to divide the sum by 2 or 2 by the sum?

  • 2

    It is not a question of order, it is a question of how the expression was put together. Note that 2/10 is totally different from 10/2, right? Even if there are parentheses, for example: 2 / (5+5) is different from (5+5) / 2. If it is an average you add the values and divide afterwards

  • If you have 2 apples and divide for 16 people, each one would receive 0.125 pieces of apple (this is because there are more people than apple). If you have 16 apples and divide to 2 people, then each person would receive 8 apples. A the value changes according to the order.

  • Correct @Valdeirpsr another good example

  • ai mds is vdde kkkk, brigade Gentee

Show 1 more comment

1

It’s printing right because 2 divided by 7+9.3 will always give average less than 5.9.

see your code run

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")
};

The correct is the inverse 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?

as in the denominator has a fraction, so we use that known rule:

repeats the first and multiplies by the inverse of the second, i.e., repeats the numerator and multiplies by the inverse of the denominator.

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

will not say that my answer is the same as the others not heim :D

0

It seems to me a question of mathematics. Sum first and divide later. I suggest using the function parseFloat from Javascript to 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")
};

  • 3

    Why did he use the function parseFloat instead of class Number?

  • tbm want to know Aaaaa explains to us Please <3

  • I prefer to use parseFloat pq if it has QQ character no number it already does the treatment.

0

The code was done correctly, only the splitter 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 it should be the opposite of 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.

Browser other questions tagged

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