Guess the outputs

Function curry
=====================
function sum() {
  let a = [1, 2, 3];
  return function (b) {
    a.push(b);
    return a;
  };
}

let result = sum();
let result2 = sum();
console.log(result2(4)); // [1,2,3,4]
Priority of Function executions
=================================

console.log(1);
setTimeout(() => {
  console.log(2);
});

let p1 = new Promise((res, rej) => {
  console.log(3);
  res();
});
p1.then(() => console.log(4));

console.log(5);

1 ==> 3 ==> 5 ==> 4 ==> 2

Do not confuse the primitive Boolean values true and false with the truthiness or falsiness of the Boolean object. Any value that is not false, undefined, null, 0, -0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition.

For example:

var b = new Boolean(false); if (b)

short circuit operator and typeof operator
============================================

console.log(0 && 1); 
// Will print 0. 
// 0 will coerced to false it will immidiately break and print 0 

console.log(23 && 24 ); //Print 24. as 23 is coreced to true

console.log(typeof 0 && 1); 
// 1 will gets printed. 
// The precedence of typeof operator is higher than && so 
// first typeof 0 returns "number" then next "number" && 1 prints 1 since 
// "number" is a valid string 

console.log(typeof (0 && 1)); // Will print number. 
// since the precedenece of the grouping elemnts (....) is higher than 
// anyother operator so (0 && 1) results in 0 due to short circuit operator
// then typeof 0 is number. 

console.log(typeof {} && 23);
// output: 23 since typeof {} ==> "object" ==> "object" && 23 ==> number

console.log(typeof ("" && 23));
//output: "string"; "" it's a falsy condtion results in a shor circuit

//Some common typeof operator behavior
console.log(typeof 10);
// expected output: "number"

console.log(typeof 0.001);
// expected output: "number"

console.log(typeof 1.23);
// expected output: "number"

console.log(typeof '');
// expected output: "string"

console.log(typeof true);
// expected output: "boolean"

console.log(typeof undeclaredVariable);
// expected output: "undefined"

console.log(typeof null);
// expected output: "object"

console.log(typeof NaN);
// expected output: "number"

console.log(typeof []);
// expected output: "object"

console.log(typeof (typeof 1));
// expected output: "string"

console.log(typeof (typeof undefined));
// expected output: "string"
//Shuffle an array 
//Solution 1 (The probability of occurrence each permutation is not uniform ):
//===============================================================================
function shuffle(arr){
arr.sort(()=> Math.random() - 0.5); 
}

// Solution 2 (Fisher-Yates algorithm) 
// (Most optimized ::
// The probability of occurrence each permutation uniformly distributed ):
//===============================================================================
function shuffle(arr) {
  // modify the arr inline to change the order randomly

   for (let i = arr.length - 1; i >= 0; i--) {
     let j = Math.floor(Math.random() * (i + 1));
     [arr[i], arr[j]] = [arr[j], arr[i]];
   }
  return arr;
}

Promise.race question

Given a promise return that promise if it's resolved within any specified time limit otherwise throw error/ do some action

// Our simulated backend response
const p1 = new Promise((resolve, reject)=> setTimeout(resolve, 600, "Got some Data!"));

const promiseTimeout = function (ms, promise) {
  // Create a promise that rejects in <ms> milliseconds
  let timeout = new Promise((resolve, reject)=> setTimeout(reject, ms, `Timeout in ${ms} ms.`));

  // Returns a race between our timeout and the passed in promise
  return Promise.race([promise, timeout]);
};

const result = promiseTimeout(500, p1);

console.log(
  result
    .then((elm) => console.log(`Success: ${elm}`))
    .catch((err) => console.log(`Error : ${err}`))
);

https://www.thatjsdude.com/interview/js2.html

sadanandpai/javascript-code-challenges

Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in Javascript