DEV Community

Cover image for Javascript Quizzes You Can't Solve
Đặng Đình Sáng
Đặng Đình Sáng

Posted on

Javascript Quizzes You Can't Solve

Q1

console.log(018 - 015);
console.log("018" - "015");
Enter fullscreen mode Exit fullscreen mode

Q2

const isTrue = true == []; 
const isFalse = true == ![]; 

console.log ( isTrue + isFalse);
Enter fullscreen mode Exit fullscreen mode

Q3

console.log(3 > 2 > 1);
Enter fullscreen mode Exit fullscreen mode

Q4

console.log(typeof typeof 1);
Enter fullscreen mode Exit fullscreen mode

Q5

console.log(('b' + 'a' + + 'a' + 'a').toLowerCase());
Enter fullscreen mode Exit fullscreen mode

Q6

console.log(typeof NaN);
Enter fullscreen mode Exit fullscreen mode

Q7

console.log(0.1 + 0.2 == 0.3);
Enter fullscreen mode Exit fullscreen mode

Q8

const numbers = [33, 2, 8];
numbers.sort();
console.log(numbers[1])
Enter fullscreen mode Exit fullscreen mode

Conclusion

What percentage of the eight questions were correct?
If you get all of them right, please leave a comment!

Ref

Top comments (4)

Collapse
 
itsjp profile image
Jaydeep Pipaliya • Edited

A1

  1. 018 is treated as a decimal (18) since it is not a valid octal number, while 015 is a valid octal (13). So, 18 - 13 = 5.
  2. "018" and "015" are converted to numbers 18 and 15. So, 18 - 15 = 3.

A2

  • true == [] converts to 1 == 0, which is false. So, isTrue is false..
  • true == ![] converts to true == false (1 == 0), which is false. So, isFalse is false.
  • false + false is 0 + 0, which equals 0. So, console.log(isTrue + isFalse); will log 0.

A3

This will log false. Here's why:

  • First, 3 > 2 evaluates to true.
  • Then, true > 1 is evaluated. In JavaScript, true is converted to 1, so 1 > 1 evaluates to false.

A4
This will log "string". Here's why:

  • typeof 1 returns "number".
  • typeof "number" returns "string".

A5

This will log "banana". Here's why:

  • 'b' + 'a' is 'ba'.
  • + 'a' is NaN because 'a' cannot be converted to a number.
  • 'ba' + NaN + 'a' results in 'baNaNa'.
  • Calling .toLowerCase() on 'baNaNa' gives 'banana'.

A6

  • This will log "number". NaN (Not-a-Number) is of type number in JavaScript.

A7

  • This will log false. Due to floating-point precision issues, 0.1 + 0.2 does not exactly equal 0.3. Instead, it equals 0.30000000000000004.

A8

  • This will log 33. The sort() method sorts the array elements as strings by default. Hence, the array becomes [2, 33, 8].
Collapse
 
untilyou58 profile image
Đặng Đình Sáng

@itsjp

A1

An octal number is a number in JavaScript that has a leading zero.
018 is regarded as a decimal number, nevertheless, because it is an incorrect octal number. 015 is 13 in octal notation. 018-015 = 18-13 = 5 as a result.

A2

To begin with, allow me to clarify the first line: true and [] are changed to integers because they are of different kinds. First to be evaluated are number(true) and number([]), which are 1 and 0, respectively. const isTrue = false since 1 == 0.

Although true and ![] are both boolean values and are not converted to integers, it appears that the second line is the same. Additionally, ![] would be false because [] is understood to be true.
const isFalse = false and const isFalse = true are the results.

The final line, console.log (isTrue + isFalse); becomes console.log (false + false); as a result of the foregoing. console.log (0 + 0); becomes a number instead than false.
Therefore, the solution is 0.

Collapse
 
itsjp profile image
Jaydeep Pipaliya

thank you for the corrections

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

😔