diff --git a/package-lock.json b/package-lock.json index d200a4d..980a6f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -112,6 +112,7 @@ "version": "7.23.2", "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.23.2.tgz", "integrity": "sha512-n7s51eWdaWZ3vGT2tD4T7J6eJs3QoBXydv7vkUM06Bf1cbVD2Kc2UrkzhiQwobfV7NwOnQXYL7UBJ5VPU+RGoQ==", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.22.13", @@ -1238,6 +1239,7 @@ "url": "https://github.com/sponsors/ai" } ], + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001541", "electron-to-chromium": "^1.4.535", diff --git a/src/01_primitives/01_getDataType.js b/src/01_primitives/01_getDataType.js index 7fd7216..a9131a4 100644 --- a/src/01_primitives/01_getDataType.js +++ b/src/01_primitives/01_getDataType.js @@ -1,11 +1,11 @@ /** * Returns the data type of the given variable. - * + * * @param {*} variable - The variable whose data type is to be determined. * @returns {string} - The data type of the variable. */ function getDataType(variable) { - // Your code here + return typeof variable; } -module.exports = getDataType; \ No newline at end of file +module.exports = getDataType; diff --git a/src/01_primitives/02_isNullObject.js b/src/01_primitives/02_isNullObject.js index 31ab68b..d2a28a3 100644 --- a/src/01_primitives/02_isNullObject.js +++ b/src/01_primitives/02_isNullObject.js @@ -1,12 +1,15 @@ /** * Returns true if the given variable is null and typeof returns 'object'. - * + * * @param {*} variable - The variable to be checked. * @returns {boolean} - True if the variable is null and typeof returns 'object'. */ function isNullObject(variable) { - // Your code here + if (variable === null && typeof variable === "object") { + return true; + } else { + return false; } - - module.exports = isNullObject; - \ No newline at end of file +} + +module.exports = isNullObject; diff --git a/src/02_scope/scopeExercise.js b/src/02_scope/scopeExercise.js index b04d6fc..e74d30e 100644 --- a/src/02_scope/scopeExercise.js +++ b/src/02_scope/scopeExercise.js @@ -1,13 +1,11 @@ function calculateProduct() { - let x = 5; + let x = 5; - if (true) { - let y = 4; - let z = 3; - } - - // Fix this line so that it returns 60 by multiplying x, y, and z - return x * z; // Should return 60 + if (true) { + let y = 4; + let z = 3; + return x * z * y; + } } module.exports = calculateProduct; diff --git a/src/02_scope/scopeExerciseHard.js b/src/02_scope/scopeExerciseHard.js index c2e5d12..30be6aa 100644 --- a/src/02_scope/scopeExerciseHard.js +++ b/src/02_scope/scopeExerciseHard.js @@ -1,21 +1,19 @@ // You can only change the scope of variables; no other changes are allowed. function mainFunction() { - let x = 10; - - function firstFunction() { - let y = 5; - if (true) { - let z = 2; - return y * z; - } + let x = 10; + let y = 5; + function firstFunction() { + if (true) { + let z = 2; + return y * z; } - - function secondFunction() { - return x + y; // y is not defined here! - } - - return firstFunction() + secondFunction(); // Should return 25 } - - module.exports = mainFunction; - \ No newline at end of file + + function secondFunction() { + return x + y; // y is not defined here! + } + + return firstFunction() + secondFunction(); // Should return 25 +} + +module.exports = mainFunction; diff --git a/src/03_closures/makeMultiplier.js b/src/03_closures/makeMultiplier.js index c7b5c64..d700093 100644 --- a/src/03_closures/makeMultiplier.js +++ b/src/03_closures/makeMultiplier.js @@ -2,15 +2,15 @@ * Create a function makeMultiplier that takes a single parameter multiplier. * The function should return another function that takes a single parameter x, * and returns the result of x multiplied by multiplier. - * + * * @param {number} multiplier - The number to multiply by. * @returns {Function} - A function that multiplies its argument by multiplier. */ function makeMultiplier(multiplier) { // Your code here - return function(x) { - // Your code here - } + return function (x) { + return multiplier * x; + }; } module.exports = makeMultiplier; diff --git a/src/04_hoisting/hoistingExercise.js b/src/04_hoisting/hoistingExercise.js index 038b2c3..f6c7a86 100644 --- a/src/04_hoisting/hoistingExercise.js +++ b/src/04_hoisting/hoistingExercise.js @@ -1,9 +1,8 @@ // Fix the code below to make the function return 12 function calculate() { - a = 4; - let a; - b = 3; - return a * b; // Should return 12 + let a = 4; + let b = 3; + return a * b; // Should return 12 } -module.exports = calculate; \ No newline at end of file +module.exports = calculate; diff --git a/src/05_logic_control_flow/findFactorial.js b/src/05_logic_control_flow/findFactorial.js index 38581d2..cd33439 100644 --- a/src/05_logic_control_flow/findFactorial.js +++ b/src/05_logic_control_flow/findFactorial.js @@ -7,7 +7,10 @@ // The factorial of 3 is 3 * 2 * 1 = 6. // Recommended: use a while of for loop to solve the problem function findFactorial(n) { - // Your code here + if (n === 0 || n === 1) { + return 1; + } + return n * findFactorial(n - 1); } -module.exports = findFactorial; \ No newline at end of file +module.exports = findFactorial; diff --git a/src/05_logic_control_flow/sumFirstNNumbers.js b/src/05_logic_control_flow/sumFirstNNumbers.js index c866a93..953bc29 100644 --- a/src/05_logic_control_flow/sumFirstNNumbers.js +++ b/src/05_logic_control_flow/sumFirstNNumbers.js @@ -8,8 +8,17 @@ * If n is negative, your function should return 0. **/ function sumFirstNNumbers(n) { - // Your code here -} + if (n <= 0) { + return 0; + } + + let sum = 0; + for (let i = 1; i <= n; i++) { + sum += i; + } + + return sum; +} -module.exports = sumFirstNNumbers; \ No newline at end of file +module.exports = sumFirstNNumbers; diff --git a/src/06_objects/compareObjects.js b/src/06_objects/compareObjects.js index 7703a2c..41fcd75 100644 --- a/src/06_objects/compareObjects.js +++ b/src/06_objects/compareObjects.js @@ -1,13 +1,44 @@ /** * Write a function that makes a deep comparison between two objects. - * Deep Comparison: two values are only considered equal if they are the same value or are objects with the same + * Deep Comparison: two values are only considered equal if they are the same value or are objects with the same * properties whose values are also equal when compared with a recursive call to deepEqual. - * @param {*} obj1 - * @param {*} obj2 + * @param {*} obj1 + * @param {*} obj2 */ function compareObjects(obj1, obj2) { - // Your code here + if (obj1 === obj2) { + return true; + } + // 2️⃣ If either is null or not an object → not equal + if ( + obj1 === null || + obj2 === null || + typeof obj1 !== "object" || + typeof obj2 !== "object" + ) { + return false; + } + + // 3️⃣ Compare number of keys + const keys1 = Object.keys(obj1); + const keys2 = Object.keys(obj2); + + if (keys1.length !== keys2.length) { + return false; + } + + // 4️⃣ Recursively compare each property + for (let key of keys1) { + if (!keys2.includes(key)) { + return false; + } + + if (!compareObjects(obj1[key], obj2[key])) { + return false; + } + } + + return true; } - + module.exports = compareObjects; - \ No newline at end of file diff --git a/src/07_arrays/reverseArray.js b/src/07_arrays/reverseArray.js index a4576a7..1d4689a 100644 --- a/src/07_arrays/reverseArray.js +++ b/src/07_arrays/reverseArray.js @@ -6,7 +6,7 @@ * @returns {Array} */ function reverseArray(arr) { - // Your code here + return arr.reverse(); } -module.exports = reverseArray; \ No newline at end of file +module.exports = reverseArray;