Skip to content

Instantly share code, notes, and snippets.

@AlejandroRodriguezP
Last active January 11, 2018 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlejandroRodriguezP/f59ccca9e7446e8f634558ad3ad5927e to your computer and use it in GitHub Desktop.
Save AlejandroRodriguezP/f59ccca9e7446e8f634558ad3ad5927e to your computer and use it in GitHub Desktop.
//PART 1
//Debug each: You are given this function each, but it doesn't work exactly as expected.
/*It should call callback on value, key, and collection respectively for each element of collection,
and accept both arrays and objects. Identify everything incorrect with each as it is provided,
and modify the function so that it works as expected. Be sure to list all that was incorrect about the original function. */
var each = function(collection, callback) {
if (typeof collection === "array"){
for (var i = 0; i < collection.length; i++) {
callback(collection[i], i, collection);
}
}
else if{
for (var key in collection) {
iterator(collection[key], key, collection);
}
}
};
//WRITE COMMENTS BELOW AND FIX CODE
//PART 2:
//Write a function called exponent that computes the exponent of a number.
// The exponent of a number says how many times the base number is used as a factor.
// 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent.
//ENSURE THE FOLLOWING TESTS PASS:
// exponent(4,3); // 64
// exponent(4,0); // 1
// exponent(4,1); // 4
// exponent(4,-2); // 1/16 or 0.0625
//RESOURCES:
//PYTHONTUTOR.COM
//MDN
//NO W3SCHOOLS OR CONSOLE!
//TIPS: ENSURE YOU PSUEDO-CODE.
//even if you don't get the solution, show your logic! that'll give you points.
//MENTION: the base case and the recursive case.
function exponent(base, exp){
//input base & exp
//if base || exp are 0 "this is the base case"
if(base === 0 || exp === 0){
return 0;
// return 0
}
else if(base < 0 || exp < 0){
exponent(base*exp)
// else if base || exp are less than 0
return exponent
// return should be negative
}
else if(base > 0 && exp > 0){
//if non apply multiply base * exp
return exponent // recursion case
//return the base * exp
}
}
//output exponent
//fill in this function with your base and recursive case
//make sure to handle your edge case of negative exponents (see above tests)
};
//PART 2:
//Write a function called exponent that computes the exponent of a number.
// The exponent of a number says how many times the base number is used as a factor.
// 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment