diff --git a/src/lessons/02-0-functions.js b/src/lessons/02-0-functions.js index 63b8e53..ef16d1f 100644 --- a/src/lessons/02-0-functions.js +++ b/src/lessons/02-0-functions.js @@ -4,13 +4,24 @@ // Define a function named 'greet' that takes one argument 'name'. // The function should console.log a greeting string: "Hello, [name]!" +function greet(name) { + console.log(`Hello, ${name}!`); +} // Define a function that squares a given number // Bind the function to a const variable named square +const square = function(num) { + if (typeof num !== 'number') return NaN; + return (num * num); +} // Define a binding named cubed whose value is a function with one parameter named num. // The function should return the value of num cubed. // The function should be defined using ES6 fat arrow syntax +const cubed = (num) => { + if (typeof num !== 'number') return NaN; + return (num * num * num); +} /** * Task 2: Bindings and Scopes @@ -21,12 +32,13 @@ function scopedFunction() { let b = 2; var c = 3; // modify the statement below to the value that scopedFunction returns true - return a + b + c === 99; + return a + b + c === 6; } const result = scopedFunction(); let d = 4; // check that 'a' exists in the global scope, if it does, set d equal to 5 +d = a ? 5 : d; /** * Task 3: Bindings and Scopes (same variable name) @@ -34,12 +46,19 @@ let d = 4; // Define a function named halve that takes one parameter named num. // The function should return num halved. +function halve(num) { + if (typeof num !== 'number') return NaN; + return (num / 2); +} // Declare a variable named num in global scope and assign it the value 100. +let num = 100; // create a variable named halfOfFifty and set it to the return value of halve(50) +const halfOfFifty = halve(50); // create a variable named halfOfNum and set it to the return value of halve(num) +const halfOfNum = halve(num); /** * Task 4: Complete the breadRecipe function @@ -58,9 +77,23 @@ const breadRecipe = function (numLoaves) { // Add ingredients using the addIngredient function // the recipe for one loaf is: 2 cups flour, 2 cups water, 1 teaspoon salt, 0.5 teaspoon yeast // example. addIngredient(2, "cup", "flour"); + + const addIngredient = (amount, unit, ingredient) => { + const ingredientAmount = amount * numLoaves; + if (ingredientAmount > 1) { + unit += "s"; + } + console.log(`${ingredientAmount} ${unit} ${ingredient}`); + } + + addIngredient(2, 'cup', 'flour'); + addIngredient(2, 'cup', 'water'); + addIngredient(1, 'teaspoon', 'salt'); + addIngredient(0.5, 'teaspoon', 'yeast'); }; // Call the breadRecipe function with 2 loaves +breadRecipe(2); /** * Task 5: Optional Parameters @@ -73,6 +106,9 @@ const breadRecipe = function (numLoaves) { // if num2 is not defined, set the default value to 0 // example: minus(2, 5) should return 3 // example: minus(5) should return -5 +function minus(num1, num2 = 0) { + return num2 - num1; +} /** * Task 6: Rest Parameters and Spread Operator @@ -84,7 +120,7 @@ const breadRecipe = function (numLoaves) { // the function should return the sum of all numbers passed in // call the function with 1, 2, 3, 4, 5 const sumRest = function (...nums) { - + return nums.reduce((sum,num) => sum += num, 0); }; // create a function named sumSpread @@ -92,7 +128,7 @@ const sumRest = function (...nums) { // Use the spread operator to calculate the sum of all numbers in the array // It should use the sumRest function defined above const sumSpread = function (nums) { - + return sumRest(...nums); }; /** @@ -104,19 +140,25 @@ const sumSpread = function (nums) { // create a function named createIdGenerator function createIdGenerator() { // create a variable named id and set it to 0 + let id = 0; // return a function that increments id and returns the new value + return function () { + id += 1; + return id; + } } // create a variable named nextId and set it to the return value of createIdGenerator +const nextId = createIdGenerator(); // create a variable named id1 and set it to the return value of nextId -let id1; +let id1 = nextId(); // create a variable named id2 and set it to the return value of nextId -let id2; +let id2 = nextId(); // create a variable named id3 and set it to the return value of nextId -let id3; +let id3 = nextId(); /** * Task 8: Closure (createGreeter) @@ -127,15 +169,22 @@ let id3; // create a function named createGreeter function createGreeter(name) { // return a function that logs "Hello, [name]!" to the console + return function() { + console.log(`Hello, ${name}!`); + } } // create a variable named greetJohn and set it to the return value of createGreeter with the name "John" +let greetJohn = createGreeter('John'); // create a variable named greetJane and set it to the return value of createGreeter with the name "Jane" +let greetJane = createGreeter('Jane'); // call greetJohn +greetJohn(); // call greetJane +greetJane(); /** * Task 9: Closure (countDown) @@ -150,8 +199,10 @@ function createCountdown(start) { return function () { if (start > 0) { // Your code here + return start--; } else { // Your code here + return 0; } }; } @@ -168,5 +219,7 @@ function power(base, exponent) { return 1; } else { // Your code here + return base * power(base, exponent - 1); } } + diff --git a/src/lessons/02-1-functions.js b/src/lessons/02-1-functions.js index f7049c8..69e9a04 100644 --- a/src/lessons/02-1-functions.js +++ b/src/lessons/02-1-functions.js @@ -13,7 +13,11 @@ * 3. Convert both minutes and seconds to strings, padding them with a leading zero if they are less than 10. * 4. Concatenate minutes and seconds with a colon (:) to form the time string. */ -let formatTime = (seconds) => {}; +let formatTime = (seconds) => { + const min = Math.floor(seconds / 60).toString().padStart(2, '0'); + const sec = (seconds % 60).toFixed(0).toString().padStart(2, '0'); + return `${min}:${sec}`; +}; /** * Creates a timer that counts up to a specified number of seconds, logging the elapsed time each second. @@ -31,4 +35,17 @@ let formatTime = (seconds) => {}; * - The elapsed time is calculated by subtracting the start time from the current time. * - Use the formatTime function to format the elapsed time before logging. */ -function countUpTo(seconds) {} +function countUpTo(seconds) { + let elapsed = 0; + let startTime = Date.now(); + const intervalId = setInterval(() => { + elapsed = Date.now() - startTime; + if (elapsed >= seconds * 1000) { + console.log(`Elapsed time: ${formatTime(elapsed / 1000)}`); + console.log("Time's up!"); + clearInterval(intervalId); + } else { + console.log(`Elapsed time: ${formatTime(elapsed / 1000)}`); + } + }, 1000); +}