简体   繁体   中英

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?

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM