Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished module 2 #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
76 changes: 57 additions & 19 deletions src/lessons/02-0-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

// 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 (x) {
return x * x;
};

// 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 = (x) => x * x * x;
/**
* Task 2: Bindings and Scopes
*/
Expand All @@ -21,26 +27,28 @@ 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();

result;
let d = 4;
// check that 'a' exists in the global scope, if it does, set d equal to 5

if (a) {
d = 5;
}
/**
* Task 3: Bindings and Scopes (same variable name)
*/

// Define a function named halve that takes one parameter named num.
// The function should return num halved.

const halve = (x) => x / 2;
// Declare a variable named num in global scope and assign it the value 100.

const 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
* Objective: Understand nested functions and scopes
Expand All @@ -49,6 +57,17 @@ let d = 4;
// Create a function named breadRecipe that takes one parameter named numLoaves
const breadRecipe = function (numLoaves) {
// Create a function named addIngredient
function addIngredient(amount, unit, ingredient) {
const amountNeeded = amount * numLoaves;
return amountNeeded > 1
? `${amountNeeded} ${unit}s ${ingredient}`
: `${amountNeeded} ${unit} ${ingredient}`;
}

console.log(`${addIngredient(2, "cup", "flour")}`);
console.log(`${addIngredient(2, "cup", "water")}`);
console.log(`${addIngredient(1, "teaspoon", "salt")}`);
console.log(`${addIngredient(0.5, "teaspoon", "yeast")}`);
// The function should take three parameters: amount, unit, ingredient
// Calculate total required ingredient amount based on the number of loaves
// The function should log the ingredient amount and name
Expand All @@ -61,7 +80,7 @@ const breadRecipe = function (numLoaves) {
};

// Call the breadRecipe function with 2 loaves

breadRecipe(2);
/**
* Task 5: Optional Parameters
*
Expand All @@ -73,6 +92,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
Expand All @@ -84,15 +106,20 @@ 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) {

let total = 0;
nums.forEach((item) => {
total += item;
});
return total;
};

console.log(sumRest(1, 2, 3, 4, 5));
// create a function named sumSpread
// the function should take one parameter name nums that is an array
// 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);
};

/**
Expand All @@ -104,19 +131,24 @@ 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 function () {
id += 1;
return id;
};
// return a function that increments id and returns the new value
}

// 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)
Expand All @@ -127,16 +159,19 @@ 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"

const greetJohn = createGreeter("John");
// create a variable named greetJane and set it to the return value of createGreeter with the name "Jane"

const greetJane = createGreeter("Jane");
// call greetJohn

greetJohn();
// call greetJane

greetJane();
/**
* Task 9: Closure (countDown)
*
Expand All @@ -150,8 +185,10 @@ function createCountdown(start) {
return function () {
if (start > 0) {
// Your code here
return start--;
} else {
// Your code here
return start;
}
};
}
Expand All @@ -167,6 +204,7 @@ function power(base, exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
// Your code here
}
}
26 changes: 24 additions & 2 deletions src/lessons/02-1-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
* 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 = 0) => {
const minutes = Math.floor(seconds / 60);
const newSeconds = Math.floor(seconds - minutes * 60);
const padd = (x) => (x < 10 ? `0${x}` : `${x}`);
return `${padd(minutes)}:${padd(newSeconds)}`;
};

console.log(formatTime(69));

/**
* Creates a timer that counts up to a specified number of seconds, logging the elapsed time each second.
Expand All @@ -31,4 +38,19 @@ 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 elapsedTime = 0;
const startTime = Date.now();
const intervalId = setInterval(() => {
elapsedTime = (Date.now() - startTime) / 1000;
if (elapsedTime < seconds) {
console.log(`Elapsed time: ${formatTime(elapsedTime)}`);
} else {
console.log(`Elapsed time: ${formatTime(elapsedTime)}`);
console.log(`Time's up!`);
clearInterval(intervalId);
}
}, 1000);
}

console.log(countUpTo(5));