Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/01_primitives/01_getDataType.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = getDataType;
13 changes: 8 additions & 5 deletions src/01_primitives/02_isNullObject.js
Original file line number Diff line number Diff line change
@@ -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;
}

module.exports = isNullObject;
14 changes: 6 additions & 8 deletions src/02_scope/scopeExercise.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 15 additions & 17 deletions src/02_scope/scopeExerciseHard.js
Original file line number Diff line number Diff line change
@@ -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;


function secondFunction() {
return x + y; // y is not defined here!
}

return firstFunction() + secondFunction(); // Should return 25
}

module.exports = mainFunction;
8 changes: 4 additions & 4 deletions src/03_closures/makeMultiplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
9 changes: 4 additions & 5 deletions src/04_hoisting/hoistingExercise.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = calculate;
7 changes: 5 additions & 2 deletions src/05_logic_control_flow/findFactorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = findFactorial;
15 changes: 12 additions & 3 deletions src/05_logic_control_flow/sumFirstNNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = sumFirstNNumbers;
43 changes: 37 additions & 6 deletions src/06_objects/compareObjects.js
Original file line number Diff line number Diff line change
@@ -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;

4 changes: 2 additions & 2 deletions src/07_arrays/reverseArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @returns {Array}
*/
function reverseArray(arr) {
// Your code here
return arr.reverse();
}

module.exports = reverseArray;
module.exports = reverseArray;