From 82db6f7685fbf2f00f5352a5bbc59992bceff138 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:55:15 +0000 Subject: [PATCH 01/10] Completed 1-fix-functions, added to 2-function-creation --- mandatory/1-fix-functions.js | 8 ++------ mandatory/2-function-creation.js | 8 +++++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mandatory/1-fix-functions.js b/mandatory/1-fix-functions.js index 6323604f..5ed2e5a8 100644 --- a/mandatory/1-fix-functions.js +++ b/mandatory/1-fix-functions.js @@ -10,9 +10,7 @@ */ -function getMood() { - let isHappy = true; - +function getMood(isHappy) { if (isHappy) { return "I am happy"; } else { @@ -21,9 +19,7 @@ function getMood() { } function greaterThan10(num) { - let isBigEnough; - - if (isBigEnough) { + if (num > 10) { return "num is greater than 10"; } else { return "num is not big enough"; diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index d4590920..0f285204 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -4,7 +4,13 @@ 1. the user should be 18 or older 2. the user must be logged in */ -function isAcceptableUser(userAge, isLoggedIn) {} +function isAcceptableUser(userAge, isLoggedIn) { + if (userAge >=18 && isLoggedIn === true) { + return true; + } else { + return false; + } +} /* Complete the function to apply discount percent based on how much is totalPrice in user cart. From 64d0bddc711f48a677299acb04849475887ae94c Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Fri, 24 Feb 2023 21:22:12 +0000 Subject: [PATCH 02/10] Completed discount, odd numbers, buy two get cheapest free --- mandatory/2-function-creation.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index 0f285204..f8914cc7 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -21,18 +21,42 @@ function isAcceptableUser(userAge, isLoggedIn) { is applieds and 142.5 should be returned) */ -function applyDiscount(totalPrice) {} +function applyDiscount(totalPrice) { + if (totalPrice >= 200) { + return totalPrice - (totalPrice * 0.1); + } else { + return totalPrice - (totalPrice * 0.05); + } +} /* 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 count = 1; + + while (count < limit) { + if (count % 2 === 1) { + console.log(count); + } + count = count + 1; + } +} /* 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 && price2) { + if (price1 >= price2) { + return price1; + } else if (price2 >= price1) { + return price2; + } + } + } + /* Complete the function to determine if it is suitable for a person to register based on their age! From 9f7a3e5e7525c6199469525385f4f2028f5f7422 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 25 Feb 2023 15:12:03 +0000 Subject: [PATCH 03/10] Added to function creation Added an if statement --- mandatory/2-function-creation.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index f8914cc7..f869c081 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -64,7 +64,11 @@ function buyTwoGetTheCheapestFree(price1, price2) { - 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`; + } +} /* Complete the function so that it prints out to the console numbers in reverse order starting at From f91009b35d5cb50a1bc35d4829ea48b45c999cfe Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 25 Feb 2023 15:18:29 +0000 Subject: [PATCH 04/10] Added an else if statement to canRegister Tested it and it works out. --- mandatory/2-function-creation.js | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index f869c081..e9b9455f 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -67,6 +67,8 @@ function buyTwoGetTheCheapestFree(price1, price2) { function canRegister(age) { if (age <= 12) { return `You Are Too Young To Register`; + } else if (age > 12 && age < 90) { + return `You Can Register`; } } diff --git a/package.json b/package.json index b70825c7..1373a338 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "jest" }, - "jest": { + "jest": { "projects": [ { "displayName": "mandatory", From adf7bf2596b853959bdd86038a3840bab3fd88e4 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 25 Feb 2023 15:19:54 +0000 Subject: [PATCH 05/10] Completed 2-function creation --- mandatory/2-function-creation.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index e9b9455f..39d462a5 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -69,6 +69,8 @@ function canRegister(age) { return `You Are Too Young To Register`; } else if (age > 12 && age < 90) { return `You Can Register`; + } else if (age >= 90) { + return `You Don't Need To Register`; } } From d7fdebf023a72a9b1be3bd404c88573b32c2afd2 Mon Sep 17 00:00:00 2001 From: mandy <109825538+m4ndycheung@users.noreply.github.com> Date: Sat, 25 Feb 2023 16:31:28 +0000 Subject: [PATCH 06/10] Update 2-function-creation.js --- mandatory/2-function-creation.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index 39d462a5..b66e31a4 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -83,7 +83,12 @@ function canRegister(age) { ) */ -function countReverse(number) {} +function countReverse(number) { + while (number >= 1) { + console.log(number); + number--; + } +} /* ======= TESTS - DO NOT MODIFY ===== */ From 856167fd4f33dff0d07664fdcf68d638a8f249f7 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Wed, 1 Mar 2023 11:50:42 +0000 Subject: [PATCH 07/10] Made changes to 2-function-creation applyDiscount answer --- mandatory/2-function-creation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index b66e31a4..674fce79 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -23,9 +23,9 @@ function isAcceptableUser(userAge, isLoggedIn) { function applyDiscount(totalPrice) { if (totalPrice >= 200) { - return totalPrice - (totalPrice * 0.1); + return totalPrice * 0.9; } else { - return totalPrice - (totalPrice * 0.05); + return totalPrice * 0.95; } } From db31bb2ba4a5a0350f5008328faae09d798b4f32 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:51:39 +0000 Subject: [PATCH 08/10] Added answer to extra --- extra/1-factorial.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/extra/1-factorial.js b/extra/1-factorial.js index da9f8e6c..9bd2c075 100644 --- a/extra/1-factorial.js +++ b/extra/1-factorial.js @@ -9,7 +9,21 @@ */ function factorial(input) { - // TODO + // 0! = 1 + // 1! = 1 + if (input === 0 || input === 1) { + return 1; + } + + // the result of the factorial calculation will be stored in here + let factorialResult = 1; + + // stops when i is greater than the input + for (i = 1; i <= input; i++) { + factorialResult = factorialResult * i; + } + + return factorialResult; } /* ======= TESTS - DO NOT MODIFY ===== */ From 7fb421e2610c803c442992be7b92179334c83f82 Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Thu, 2 Mar 2023 16:55:37 +0000 Subject: [PATCH 09/10] Amended answer to 2-function-creation Took on Michael's feedback to write count = count + 1 in a shorter way --- mandatory/2-function-creation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index 674fce79..1fa336f8 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -39,7 +39,7 @@ function printOddNumbers(limit) { if (count % 2 === 1) { console.log(count); } - count = count + 1; + count++; } } From 61ad3d1e17e1d7fad73d3dc865044cc46cf7509a Mon Sep 17 00:00:00 2001 From: mandy-cheung <109825538+m4ndycheung@users.noreply.github.com> Date: Thu, 2 Mar 2023 17:09:40 +0000 Subject: [PATCH 10/10] Updated answers for 2-function-creation Took on Michael's feedback and added indentation to printOddNumbers. Also declared a let variable for countReverse so that input data won't be modified --- mandatory/2-function-creation.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mandatory/2-function-creation.js b/mandatory/2-function-creation.js index 1fa336f8..ced085ed 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -37,7 +37,7 @@ function printOddNumbers(limit) { while (count < limit) { if (count % 2 === 1) { - console.log(count); + console.log(count); } count++; } @@ -84,9 +84,11 @@ function canRegister(age) { */ function countReverse(number) { - while (number >= 1) { - console.log(number); - number--; + let currentNumber = number; + + while (currentNumber >= 1) { + console.log(currentNumber); + currentNumber--; } }