feat: recursion

This commit is contained in:
Mike 2024-01-24 07:34:04 -05:00
parent 5e71ed17da
commit ac9bd268d5
5 changed files with 161 additions and 0 deletions

View file

@ -0,0 +1,93 @@
let nestedObject = {
data: {
info: {
stuff: {
thing: {
moreStuff: {
magicNumber: 44,
something: "foo2",
else: "fool",
here: "fo",
nested: {
again: "bob",
nestedNested: {
free: "beer",
boozy: "chicks",
},
},
},
},
},
},
},
};
function contains(obj, searchValue) {
if (Object.keys(obj).length === 0) {
return false;
}
let temp = { ...obj };
let keys = Object.keys(temp);
let key = keys.shift();
if (typeof obj[key] === "object") {
return contains(obj[key], searchValue);
}
if (obj[key] === searchValue) {
return true;
} else {
if (keys.length >= 1) {
function checkValues(object, keys, searchValue) {
if (keys.length < 1) return false;
let temp = keys.shift();
if (object[temp] === searchValue) return true;
if (typeof object[temp] === "object") {
return contains(object[temp], searchValue);
}
return checkValues(object, keys, searchValue);
}
return checkValues(obj, keys, searchValue);
}
return contains(obj, searchValue);
}
}
let hasIt = contains(nestedObject, 44); // true
let doesntHaveIt = contains(nestedObject, "foo"); // false
let doesntHaveItEither = contains(nestedObject, "chicks"); // true
console.log(hasIt, doesntHaveIt, doesntHaveItEither);
function chatgptContains(obj, searchValue) {
const keys = Object.keys(obj);
const checkKeys = (keys) => {
if (keys.length === 0) {
return false;
}
const key = keys.shift();
if (obj[key] === searchValue) {
return true;
}
if (
typeof obj[key] === "object" &&
chatgptContains(obj[key], searchValue)
) {
return true;
}
return checkKeys(keys);
};
return checkKeys(keys);
}
let chatgptHasIt = chatgptContains(nestedObject, 44); // true
let chatgptDoesntHaveIt = chatgptContains(nestedObject, "foo"); // false
let chatgptDoesntHaveItEither = chatgptContains(nestedObject, "chicks"); // true
console.log(chatgptHasIt, chatgptDoesntHaveIt, chatgptDoesntHaveItEither);

View file

@ -0,0 +1,20 @@
let seven = totalIntegers([[[5], 3], 0, 2, ["foo"], [], [4, [5, 6]]]); // 7
function totalIntegers(arr) {
let total = 0;
if (arr.length === 0) return 0;
if (typeof arr !== "number") {
let numb = arr.shift();
if (typeof numb === "number") {
total += 1;
} else if (Array.isArray(numb)) {
total += totalIntegers(numb);
}
}
return (total += totalIntegers(arr));
}
console.log(seven);

View file

@ -0,0 +1,27 @@
function SumSquares(arr) {
let sum = 0;
if (arr.length === 0) return 0;
let first = arr.shift();
if (Number.isInteger(first)) {
sum = first * first;
} else if (Array.isArray(first)) {
sum += SumSquares(first);
}
return (sum += SumSquares(arr));
}
var l = [1, 2, 3];
console.log(SumSquares(l)); // 1 + 4 + 9 = 14
l = [[1, 2], 3];
console.log(SumSquares(l)); // 1 + 4 + 9 = 14
l = [[[[[[[[[1]]]]]]]]];
console.log(SumSquares(l)); // 1 = 1
l = [10, [[10], 10], [10]];
console.log(SumSquares(l)); // 100 + 100 + 100 + 100 = 400

View file

@ -0,0 +1,20 @@
function replicate(multiple, number) {
if (multiple <= 0) return [];
// let arr = [];
// original and too long
// if (multiple >= 1) {
// arr.push(number);
// } else {
// arr.push(replicate(multiple - 1), number);
// }
// const concated = arr.concat(replicate(multiple - 1, number));
// return concated;
return [number].concat(replicate(multiple - 1, number));
}
console.log(replicate(3, 5)); // [5, 5, 5]
console.log(replicate(1, 69)); // [69]
console.log(replicate(-2, 6)); // []
console.log(replicate(60, 30));

View file

@ -0,0 +1 @@
https://www.codingame.com/playgrounds/5422/js-interview-prep-recursion