diff --git a/javascript/recursion/q5.js b/javascript/recursion/q5.js new file mode 100644 index 0000000..ea6b0f0 --- /dev/null +++ b/javascript/recursion/q5.js @@ -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); diff --git a/javascript/recursion/q7.js b/javascript/recursion/q7.js new file mode 100644 index 0000000..7da79af --- /dev/null +++ b/javascript/recursion/q7.js @@ -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); diff --git a/javascript/recursion/q8.js b/javascript/recursion/q8.js new file mode 100644 index 0000000..2ef3611 --- /dev/null +++ b/javascript/recursion/q8.js @@ -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 diff --git a/javascript/recursion/q9.js b/javascript/recursion/q9.js new file mode 100644 index 0000000..cd96e68 --- /dev/null +++ b/javascript/recursion/q9.js @@ -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)); diff --git a/javascript/recursion/recursion_questions.txt b/javascript/recursion/recursion_questions.txt new file mode 100644 index 0000000..01bda48 --- /dev/null +++ b/javascript/recursion/recursion_questions.txt @@ -0,0 +1 @@ +https://www.codingame.com/playgrounds/5422/js-interview-prep-recursion