LearnFrenzy
Sign in Take a free test

Operators · Exercise 4 of 4

Operators Operators Coding Problems

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

JavaScript programs that use operators well: a calculator, parity and power-of-two checks with bitwise operators, swapping, digit reversal, counting set bits and merging defaults with ?? and ?.. Write each one yourself first, then open the solution – every solution was run on Node.js and the output shown is the real output.

Question 1 Easy

Write calculate(a, op, b) for +, -, *, /, % and **, returning "Cannot divide by zero" for division by 0 and "Unknown operator" for anything else.

Question page & discussion
Question 2 Easy

Check whether each of 4, 7, -3, 0 and 15 is even or odd, first with % and then with the bitwise AND operator, and confirm both agree.

Question page & discussion
Question 3 Medium

Swap two integers three ways without a temporary variable: with arithmetic, with XOR and with destructuring.

Question page & discussion
Question 4 Easy

Starting from a basic salary of ₹40,000, add 20% HRA and 12% DA, subtract ₹1,800 PF and 10% tax on the gross, using compound assignment operators.

Question page & discussion
Question 5 Medium

Write isPowerOfTwo(n) using the bitwise trick n & (n − 1), and test it on 1, 2, 6, 16, 0, 64 and 100.

Question page & discussion
Question 6 Medium

Count the number of 1s in the binary form of 0, 7, 10, 255 and 1023 using bitwise operators (Brian Kernighan's method).

Question page & discussion
Question 7 Medium

Build the final settings from a user object that may be missing values (or be entirely null), keeping valid falsy values such as 0, "" and false.

Question page & discussion
Question 8 Easy

Reverse the digits of 12345, -907 and 1200 using % and Math.trunc (no strings), keeping the sign.

Question page & discussion
Question 9 Easy

Find the largest of three numbers using only the ternary operator, and check the result against Math.max for several triples.

Question page & discussion
Question 10 Hard

Evaluate a set of tricky expressions (precedence, coercion and short-circuiting) and print each with its value and type, to check your own predictions.

Question page & discussion
All exercises doneBack to OperatorsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links