Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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
6 changes: 2 additions & 4 deletions mandatory/1-fix-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

*/

function getMood() {
let isHappy = true;

function getMood(isHappy) {
if (isHappy) {
return "I am happy";
} else {
Comment on lines 14 to 16
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a pattern in programming to simplify if .. else statements. You can rewrite this block as follows:

 if (isHappy) {
    return "I am happy";
  }

return "I am not happy";

the pattern is called return early pattern, and you can read more here https://medium.com/swlh/return-early-pattern-3d18a41bba8

Expand All @@ -21,7 +19,7 @@ function getMood() {
}

function greaterThan10(num) {
let isBigEnough;
let isBigEnough = num > 10;

if (isBigEnough) {
return "num is greater than 10";
Expand Down
70 changes: 63 additions & 7 deletions mandatory/2-function-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,84 @@
1. the user should be 18 or older
2. the user must be logged in
*/
function isAcceptableUser(userAge, isLoggedIn) {}
function isAcceptableUser(userAge, isLoggedIn) {
return userAge >= 18 && isLoggedIn;
}

const loggedIn = true;
const userAge = 21;

if (isAcceptableUser(userAge, loggedIn)) {
console.log("User is acceptable.");
} else {
console.log("User is not acceptable.");
}

/*
Complete the function to apply discount percent based on how much is totalPrice in user cart.
- When the total price is greater than 200, a 10% discount should be applied
- When the total price is less than 200, a 5% discount should be applied

The function should return the new price to be paid (e.g. if the totalPrice is 150, a 5% discount
is applieds and 142.5 should be returned)
is applied and 142.5 should be returned)
*/

function applyDiscount(totalPrice) {}
function applyDiscount(totalPrice) {
let discountPercent;
if(totalPrice > 200) {
discountPercent = 0.1;
} else {
discountPercent = 0.05;
}
let discountAmount = totalPrice * discountPercent;
let newPrice = totalPrice - discountAmount;
return newPrice;
}

/*
Complete the function to print to the console the odd numbers between 1 and limit (use a while loop):
*/
function printOddNumbers(limit) {}
function printOddNumbers(limit) {
let i = 1;
while (i <= limit) {
if(i % 2 !==0) {
console.log(i);
}
i++;
}
}
Comment on lines +44 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works fine, but you could make it 2x more performant by skipping over even numbers. For example:

Suggested change
function printOddNumbers(limit) {
let i = 1;
while (i <= limit) {
if(i % 2 !==0) {
console.log(i);
}
i++;
}
}
function printOddNumbers(limit) {
let i = 1;
while (i <= limit) {
console.log(i);
i+=2;
}
}


/*
Complete the buyTwoGetTheCheapestFree function: if user buys two items, the cheapest item will be free!
The function should return the price to be paid once the discount is applied.
*/
function buyTwoGetTheCheapestFree(price1, price2) {}
function buyTwoGetTheCheapestFree(price1, price2) {
if (price1 < 0 || price2 < 0) {
return "Invalid price for an item";
}
Comment on lines +59 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice add, always good to handle edge cases even if the exercise didn't mention it


let totalPrice = price1 + price2;
let discount = Math.min(price1, price2);
let discountedPrice = totalPrice - discount;

return discountedPrice;
Comment on lines +63 to +67
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified with the following:

Suggested change
let totalPrice = price1 + price2;
let discount = Math.min(price1, price2);
let discountedPrice = totalPrice - discount;
return discountedPrice;
return Math.max(price1, price2);

What do you think?

}

/*
Complete the function to determine if it is suitable for a person to register based on their age!
- if the person is 12 or younger it should return "You Are Too Young To Register"
- if the person is older than 12 and younger than 90 it should return "You Can Register"
- if the person is 90 or older it should return "You Don't Need To Register"
*/
function canRegister(age) {}
function canRegister(age) {
if (age <= 12) {
return "You Are Too Young To Register";
} else if (age < 90) {
return "You Can Register";
} else {
return "You Don't Need To Register";
}
}

/*
Complete the function so that it prints out to the console numbers in reverse order starting at
Expand All @@ -45,7 +92,16 @@ function canRegister(age) {}
)
*/

function countReverse(number) {}
function countReverse(number) {
if (number <= 0) {
console.log("Please provide a positive number");
return;
}
for (let i = number; i >= 1; i--) {
console.log(i);
}

}

/* ======= TESTS - DO NOT MODIFY ===== */

Expand Down