DEV Community

loading...

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
__victorchan profile image
Victor Chan

Heres my solution in Javascript:
It seems like it always returns apple, pretty neat question huh (Unless im wrong? 😅)

const SubtractSum = return () => "apple"

How I found this out:

const fruits = [
"kiwi",
  "pear",
  "kiwi",
  "banana",
...
 "melon",
  "pineapple",
  "apple",
  "pineapple",
]

const SubtractSum = x => {
  const sum = `${x}`.split("").reduce((p, c) => +p + +c, 0)
  const y = x - sum
  return y <= 99 ? fruits[y - 1] : SubtractSum(y)
}

//Testing out all possible combinations 10 -- 10000
for (let i = 10; i < 10000; i++) {
  console.log(i, SubtractSum(i)) //Prints "apple"
}