codingcali
codingcali

Reputation: 33

How do I solve this ? Javascript

Here is the problem:

Declare a variable called x and assign it a value that equals the remainder of 20 divided by 3. Next, divide and assign 1 to that variable and display the result in an alert box. The result should be 2.

Here is my code:

var x = 20 / 3;
alert(x / 1);

I am brand new to Javascript. I am coming up with 6.66. The answer should be 2. What am I doing wrong?

Upvotes: 2

Views: 71

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44125

You need this:

the remainder of 20 divided by 3

You're dividing. The remainder (or modulo) operator in JavaScript is the percentage symbol (%).

So your code should look like this instead:

var x = 20 % 3;
alert(x / 1);

Further reading:

Upvotes: 5

Muhammad Usman
Muhammad Usman

Reputation: 10148

Well there is % in JS for remainder. / is the division sign.

var x = 20 % 3; console.log(x / 1);

Upvotes: 2

Related Questions