Q:

Write a JavaScript program to build an array, using an iterator function and an initial seed value

0

Write a JavaScript program to build an array, using an iterator function and an initial seed value.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const unfold = (fn, seed) => {
  let result = [],
    val = [null, seed];
  while ((val = fn(val[1]))) result.push(val[0]);
  return result;
};
var f = n => (n > 50 ? false : [-n, n + 10]);
console.log(unfold(f, 10));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now