Skip to content

Instantly share code, notes, and snippets.

@carlssonk
Last active February 1, 2022 19:53
Show Gist options
  • Save carlssonk/6fd0216324ff24e5cee2b804298e892c to your computer and use it in GitHub Desktop.
Save carlssonk/6fd0216324ff24e5cee2b804298e892c to your computer and use it in GitHub Desktop.
bigfrontend.dev coding problems with answers.

Coding problems w/ solutions

Most of these questions are taken from bigfrontend.dev. A great website for practicing JavaScript and preparing for frontend interviews.

  1. Implement curry()
function curry(fn) {
  return function curryInner(...args) {
    if (args.length >= fn.length) return fn(...args);
    return (...args2) => curryInner(...args, ...args2);
  };
}

// USAGE ↓

// Utility function that will be curried
const log = (date, importance, message) => {
   return `${date}:${importance}:${message}`
}

const curryLog = curry(log)

console.log(curryLog(new Date(), "INFO", "hello world")) // date:INFO:hello world
console.log(curryLog(new Date())("INFO")("hello world")) // date:INFO:hello world
const logNow = curryLog(new Date())
console.log(logNow("INFO", "hello world"))               // date:INFO:hello world
  1. create a sum
const sum = (num) => {
  const func = (num2) => {
    return num2 ? sum(num+num2) : num
  }	
  func.valueOf = () => num
  return func;
}

// USAGE ↓

const sum1 = sum(1)

console.log(sum1(3).valueOf())  // 4
console.log(sum1(1) == 2)       // true
console.log(sum(1)(2)(3) == 6)  // true
  1. remove characters
const removeChars = (input) => {
  const regExp = /b|ac/g
  while (regExp.test(input)) {
    input = input.replace(regExp, "")
  }
  return input
}

// USAGE ↓

removeChars("ab") // "a"
removeChars("abc") // ""
removeChars("cabbaabcca") // "caa"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment