-1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
console.log(1 - 3 * 2);
</script>
</body>
</html>
I’m confused in the code above returning me a negative and not positive number because it follows my logic was not for him to do multiplication first, since in this case multiplication comes first, so it was not for him to do 3 * 2
that will give 6
and then subtract 1
that will give 5
? and even putting the parentheses in order to force it to do the multiplication first and then subtract it keeps returning negative number:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
console.log(1 - (3 * 2));
</script>
</body>
</html>
Instead of trying to make self-contained whole HTML files to run a single line of program, there is an interactive Javascript prompt in the browsers - exactly n console where you see the output of "console.log". I suggest you play a little with mathematical expressions and string, as well as other basic structures, before trying to make whole programs - understanding happens much easier.
– jsbueno
I voted to close because it’s a purely mathematical question, not a programming question. If you disagree, please make your argument here: https://pt.meta.stackoverflow.com/q/7731/112052
– hkotsubo
@hkotsubo I voted to close based on the mistaken interpretation of commutativity of subtraction in Reals. But what he put, to close because it is a purely mathematical question, is well-founded because to give an exact and adequate answer to this question would require an extensive algebraic demonstration on homomorphism and commutative bodies that goes far beyond the scope of the site
– Augusto Vasques
When you multiply
3 * 2
will give6
, but you’re taking6
of1
just imagine a horizontal line with positive and negative numbers-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6
when you subtract6
of1
He’s gonna fall right into-6
in which this will be the result of the account, to be able to return a positive number just do the opposite6 * 2 - 1
.– user170164
@Augusto Vasques, what is algebraic?
– user168020
@hkotsubo, not quiet!
– user168020
@ledeveloper,algebraic is derivative of Algebra, the field of mathematics that studies the formalities of numerical groupings such as sets, rings, vector spaces, magmas, monoids,... and the transformations applicable to these fields.
– Augusto Vasques