FAQ: Introduction to JavaScript - Arithmetic Operators

Are you sure you understand math nomenclature? Left divides into right. 2/1 is 1 divided by 2, or 2 divided into 1. (or just 2) Codeacademy is using incorrect phasing.

Well, not in the way you do, one supposes.

The common operator for division is the forward slash. It fills in on one line for the rational expression (a fraction) that would consist of a numerator, a division bar, and a denominator.

 numerator
-----------    //  ratio
denominator

dividend
--------    //  quotient
 divisor

In long division, left does divide into right, that is correct, but notice where the operands are place…

          quotient
        ____________
divisor )  dividend
         - quotient X divisor
         -----------
           remainder
25 / 5  =>  25 divided by 5  =>  5 divided into 25  =>

5 ) 25  |  5 with 0 remainder
2 Likes

Hi!
This is more related to the functionality of the coding environment. As I was attempting to perform task 2 of “Arithmetic Operator” that asks " On a new line write another console.log() . Inside the parentheses, take the current year and subtract 1969" when I run my answer a gray x appears to the side of the second task and does not let me go forward in the course. I know I am typing the code correctly:

console.log(2021-1969);

What could be the issue? Thanks for the help.

Please post a link to the exercise so we can see if there may be a problem.

Thank you, here it is:

https://www.codecademy.com/courses/introduction-to-javascript/lessons/introduction-to-javascript/exercises/math-operators

I already tried in another laptop to see if it was a problem with my system but the problem still persists. In looking at the forums I noticed several people having this issue but with different tasks in other courses, the posts have not been addressed by codecademy’s moderators.

Thank you in advance!

1 Like

Having Reset the lesson, I got all green lights with,

console.log(67+3.5)
console.log(2022-1969)
console.log(65/240)
console.log(0.2708*100)

Could be the lesson checker is looking for the current year.

Thanks for your help, unfortunately I had tried already resetting the code with 2022 and I still get the same gray X box. I do get the correct answer. Please see the picture attached. Should I open a support ticket? please let me know.

One doubts anything will get looked at any time soon. Scroll down to the next questions and finish them (or paste in my solution from above) and see if that sorts it out. Else, just get the solution so you can move on.

1 Like

Right on! Thank you very much for your help. In trying to be honest with my progress I got all complicated with this. I will sure keep your solution in mind if the issue happen later in another course.

Thank you!

1 Like

Hi, I’m new to JS and I’m learning about Arithmetic Operators. I understand it well, I was just wondering what are the applications for using remainders?

The simplest application is for determining divisibility, and in the case of the number 2, parity.

n % 2

will mean n is odd if the result is 1, and even when the result is 0.

n % 5

will mean n is divisible by 5 when the result is 0, otherwise it is not evenly divisible by 5.

Another useful way to implement remainders is in setting up N groups of individuals or items. If every one is given a unique sequential number, we can split the group into N groups by the remainder.

27 % 4

The one with the number 27 will be assigned to group 3 since that is the remainder.

2 Likes

Amazing, thanks for this

1 Like

remove the quotation marks " " from the ‘‘console.log(240/65)’’.

put this code inside and run

console.log(240/65)

Hello
what about the priority of operations ?
Trying (for fun) to answer in line 4 console.log(65/240*100) showed me the *100 was considered as numerator, what if I had wanted to multiply 240 by 100 in the denominator?
What about additions or subtractions inside numerators or denominators?
Last question, how to ask for the integral value of a decimal number ? ex: how to get the answer 3 to the question 10/3 ?
Thank you very much

Operator precedence, or ‘order of operations’ gives priority to,

  • brackets
  • exponents (including logarithms)
  • multiplication and division
  • addition and subtraction

Note that inverse operations have the same operator precedence.

  • log is the inverse of exponent
  • division is the inverse of multiplication
  • subtraction is the inverse of addition.

In light of this, operations of equal precedence are handled from left to right.

66 - 55 + 11  is 22 in any order.
66 + 11 - 55
-55 + 11 + 66

If you are unsure of how a language is going to handle your complex operation, make it easier for the program by assigning variables.

a = 65
b = 240 * 100
c = a / b

or, use brackets:

c = 65 / (240 * 100)

As for integer division, in your last question, the simplest way is to use Math.floor():

Math.floor(10 / 3)  //  3

JavaScript does not have an operator for floor division, hence the need for a Math method. If you are coming from a Python background, you will have been used to that operator (perhaps) but that is another language.

JS also does not have an ‘integer’ class, only Number, so every number is a float, no matter what. If we want an integer, then apply ‘floor()’ to any division. For multiplication we may wish to round up or down, according to the 4/5 rule, in which case we would use Math.round(). To always round up, use, Math.ceil().

Lastly, if all we want is a representation, leaving, a = 10 / 3 as a float, we can use the, .toFixed() method, which will return a string.

a = 10 / 3
a.toFixed(0)    //  '3'
1 Like

Just discovering coding lessons for the first time. Thank you very much for your kind and fast support.

All the best

1 Like

When you divide 5 by 20, the quotient is 0 (because 20 doesn’t go into 5 at all), and the remainder is 5. So 5 % 20 equals 5, not 0.

In general, if a is divided by b and b doesn’t evenly divide a, then a % b will yield the remainder. If a is less than b, then a % b will always be a.

Hello just two more questions there:

  1. if I would (poorly) write (a * b / c * d) in the console, will it instead give the result to (a * b * d) / c ?
  2. if I would send the result a.toFixed(0) (ex: a = 10 / 3) to a variable b:
    b = a.toFixed(0)
    if I understand correctly, I will not be able to use b as a number for arithmetic operations as it will just be a string of characters ‘3’ and not a number anymore ?

Thank you very much

if I would (poorly) write (a * b / c * d) in the console, will it instead give the result to (a * b * d) / c ?

The operator precedence for multiplication and division are the same, so the order is irrelevant here and both expressions are equivalent to each other.

The toFixed() method in JavaScript returns a string representation of a number with a specified number of decimal places. So when you assign the result of a.toFixed(0) to a variable b , b will indeed be a string containing the rounded value of a .

1 Like