Javascript please show me how to do these two exercises and comment so I can understand what's happening. Thank you.
// Execise #1
/*
Modify the function so that it returns the length of an array recursively, without using the .length property. Use recursion!
Hint: when you index outside of an array, it returns undefined
*/
function getLength(array, i = 0) {
}
// To check if you've completed the challenge, uncomment these console.logs!
// console.log(getLength([1])); // -> 1
// console.log(getLength([1, 2])); // -> 2
// console.log(getLength([1, 2, 3, 4, 5])); // -> 5
// Exercise #2
/*
Modify the function so that it returns true if a number is even (and false if it's not) assume input will be a positive integer Use recursion!
*/
function isEven(n) {
}
// To check if you've completed the challenge, uncomment these console.logs!
// console.log(isEven(0)); // -> true
// console.log(isEven(5)); // -> false
// console.log(isEven(10)); // -> true
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
function getLength(array) {
return 0 in array ? 1 + getLength(array.slice(1)) : 0;
}
function isEven(n) {
if (n === 0) return true;
if (n === 1) return false;
return isEven(n - 2);
}
Get Answers For Free
Most questions answered within 1 hours.