DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Michal Bryxí
Michal Bryxí

Posted on

JavaScript braces quiz

My favourite example of a perfectly valid JavaScript code that shows all the weird stuff that's possible to do with braces, parentheses and few of the ES6 syntax additions:

let a = 1;
let b = 10;
let c = 100;

({ b: a = ++c } = (({ b: c = a } = { c: { b: ++a } }) => ({ b: ++c }))({ b: ++c }));

console.log(a, b, c);

Quite an extreme interview question if you ask me. But can you figure out, using just your head, what will be the output in the terminal?

Top comments (1)

Collapse
 
pengeszikra profile image
Peter Vivo

Nice quiz

// at first remove outer dependency:

let quiz = (a, b, c) => ({ b: a = ++c } = (({ b: c = a } = { c: { b: ++a } }) => ({ b: ++c }))({ b: ++c }));

// I tested:

let strictEqual = (first, ...rest) => !rest.find( result => JSON.stringify(result) !== JSON.stringify(first))


strictEqual( quiz(1, 10, 100) , quiz(2, 28, 100) ) // true

// so my solution is:

 let solution = n => ({b:+n+2})

// test

strictEqual(quiz(1, 10, 100), quiz(null, null, 100), solution(100) )

strictEqual(quiz(1, 10, 'one'), quiz(undefined, null, 'one'), solution('one') )
Enter fullscreen mode Exit fullscreen mode

Join the One Year Club

You can earn this badge by being a registered member of the DEV Community for at least one year. Create an account and get started today.