DEV Community

Yanze Dai for Pitayan Blog

Posted on • Originally published at pitayan.com on

1

8 Javascript quiz that may confuse you

Originally published on Pitayan | 8 Javascript quiz that may confuse you


These days, I was preparing a small game for our team’s tech workshop. Thought it’d be a good opportunity of introducing the some fundamental and tricky stuffs around JavaScript. So I made 8 quiz to our team members. And hope they could solve them within 15 min. Eventually, it took all of them over 20 minutes to complete and most of them could solve 4-5 questions correctly.

You can take it as just a small test, each quiz has answer attached to the end of the code. Try to answer them first and then look at the answers. Good luck.

#What do these console.log print out?

#No. 1 – Doctor Pavlov has a dog

function Animal(){ 
  this.type = "animal"
}

function Dog(){ 
  this.name = "dog"
}

Dog.prototype = new Animal()

var PavlovPet = new Dog(); 

console.log(PavlovPet. __proto__ === Dog.prototype)
console.log(Dog.prototype. __proto__ === Animal.prototype)

Enter fullscreen mode Exit fullscreen mode

Answer for #1

#No. 2 – Be careful with the “sort”

var arr = [5, 22, 14, 9];

console.log(arr.sort());

Enter fullscreen mode Exit fullscreen mode

Answer for #2

#No. 3 – Closure and event loop

for (var i = 0; i < 3; i++) {
  const log = () => {
    console.log(i)
  }
  setTimeout(log, 100)
}

Enter fullscreen mode Exit fullscreen mode

Answer for #3

#No. 4 – There’s indentation

function createNewArray(item) {
  return
    [item]
}

console.log(createNewArray(0))

Enter fullscreen mode Exit fullscreen mode

Answer for #4

#No. 5 – What’s inside the “numbers”

const length = 4
const numbers = []
for (var i = 0; i < length; i++);{
  numbers.push(i + 1)
}

console.log(numbers)

Enter fullscreen mode Exit fullscreen mode

Answer for #5

#No. 6 – No length

const clothes = ['shirt', 'socks', 'jacket', 'pants', 'hat']
clothes.length = 0

console.log(clothes[3])

Enter fullscreen mode Exit fullscreen mode

Answer for #6

#No. 7 – Variable went crazy

var a = 1
function output () {
    console.log(a)
    var a = 2
    console.log(a)
}
console.log(a)
output()
console.log(a)

Enter fullscreen mode Exit fullscreen mode

Answer for #7

#No. 8 – There’s an accidental declaration

function foo() {
    let a = b = 0
    a++
    return a
}

foo()
console.log(typeof a)
console.log(typeof b)

Enter fullscreen mode Exit fullscreen mode

Answer for #8

#In the end

Thanks so much for reading! Have you got them all correct?

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (1)

Collapse
 
andrewbaisden profile image
Andrew Baisden

😂

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay