Skip to content

Latest commit

 

History

History
164 lines (133 loc) · 2.99 KB

File metadata and controls

164 lines (133 loc) · 2.99 KB

Exercise

Write a function called power which takes in a base and an exponent. If the exponent is 0, return 1.

Sample

SAMPLE: power(2, 4); // 16
power(2, 3); // 8
power(2, 2); // 4
power(2, 1); // 2
power(2, 0); // 1

Answer

function power(base, exp) {
  if (exp === 0) return 1;
  return base * power(base, --exp);
}

Exercise

Write a function that returns the factorial of a number. As a quick refresher, a factorial of a number is the result of that number multiplied by the number before it, and the number before that number, and so on, until you reach 1. The factorial of 1 is just 1.

Sample

factorial(5); // 5 * 4 * 3 * 2 * 1 === 120

Answer

function factorial(value) {
  if (value === 1) return 1;
  return value * factorial(--value);
}
factorial(5);

Exercise

Write a function called all which accepts an array and a callback and returns true if every value in the array returns true when passed as parameter to the callback function

Sample

var allAreLessThanSeven = all([1, 2, 9], function(num) {
  return num < 7;
}); // false

Answer

function all([value, ...values], test) {
  if (!test(value)) return false;
  if (values.length === 0) return true;
  return all(values, test);
}
var allAreLessThanSeven = all([1, 2, 3], function(num) {
  return num < 7;
});

Exercise

Write a function called productOfArray which takes in an array of numbers and returns the product of them all

Sample

var six = productOfArray([1, 2, 3]); // 6
var sixty = productOfArray([1, 2, 3, 10]); // 60

Answer

function productOfArray(array) {
  if (array.length === 0) return 1;
  return array.shift() * productOfArray(array);
}
productOfArray([1, 2, 3, 10]); // 60

Exercise

Write a function called contains that searches for a value in a nested object. It returns true if the object contains that value.

Sample

var nestedObject = {
  data: {
    info: {
      stuff: {
        thing: {
          moreStuff: {
            magicNumber: 44,
            something: "foo2"
          }
        }
      }
    }
  }
};

var nestedObject = {
  data: {
    info: {
      stuff: {
        thing: {
          moreStuff: {
            magicNumber: 44,
            something: "foo2"
          }
        }
      }
    }
  }
};
let hasIt = contains(nestedObject, 44); // true
let doesntHaveIt = contains(nestedObject, "foo"); // false

Answer

var nestedObject = {
  data: {
    info: {
      stuff: {
        thing: {
          moreStuff: {
            magicNumber: 44,
            something: "foo2"
          }
        }
      }
    }
  }
};

function contains(obj, valueToSearch) {
  const isObject = typeof obj === "object";
  if (isObject) {
    for (var key in obj) {
      const result = contains(obj[key], valueToSearch);
      if (result) return true;
    }
  } else {
    return obj === valueToSearch;
  }
  return false;
}
contains(nestedObject, 44); // true
contains(nestedObject, "foo"); // false