From 3493176166667b5f51955e19e7accdff5ba84b82 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Tue, 28 Feb 2023 19:27:31 +0000 Subject: [PATCH 1/3] Completed Mandatory part --- mandatory/1-fix-functions.js | 8 ++--- mandatory/2-function-creation.js | 57 ++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/mandatory/1-fix-functions.js b/mandatory/1-fix-functions.js index 6323604f..2f717941 100644 --- a/mandatory/1-fix-functions.js +++ b/mandatory/1-fix-functions.js @@ -10,20 +10,18 @@ */ -function getMood() { - let isHappy = true; +function getMood(isHappy) { if (isHappy) { return "I am happy"; - } else { + } else { return "I am not happy"; } } 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..f8f648b7 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) { + return true; + } else { + return false; + } +} /* Complete the function to apply discount percent based on how much is totalPrice in user cart. @@ -15,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 0.9 * totalPrice; + } else if (totalPrice > 0 && totalPrice < 200) { + return 0.95 * totalPrice; + } else { + return 0 + } +} /* 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 = 0; + while ( i <= limit) { + if (i % 2 === 1) { + console.log(i); + } + i = i + 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) { + return price1; + } else if (price2 > price1) { + return price2; + } else price1; +} /* Complete the function to determine if it is suitable for a person to register based on their age! @@ -34,7 +64,17 @@ 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"; + } else if (age > 12 && age < 90) { + return "You Can Register"; + } else if (age >= 90) { + return "You Don't Need To Register"; + } else { + return false; + } +} /* Complete the function so that it prints out to the console numbers in reverse order starting at @@ -45,7 +85,12 @@ function canRegister(age) {} ) */ -function countReverse(number) {} +function countReverse(number) { + while (number > 0) { + console.log(number); + number = number - 1; + } +} /* ======= TESTS - DO NOT MODIFY ===== */ From 29707efb8d7bddc423ce6da39a8f677708fd6907 Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Wed, 1 Mar 2023 19:03:52 +0000 Subject: [PATCH 2/3] Update 1-factorial.js --- extra/1-factorial.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extra/1-factorial.js b/extra/1-factorial.js index da9f8e6c..2b1807b5 100644 --- a/extra/1-factorial.js +++ b/extra/1-factorial.js @@ -10,6 +10,16 @@ function factorial(input) { // TODO + let j = i + 1; + let k = 1; + + for (i = 0; i <= input; i++) { + while (j <= input) { + return j; + } + j = j + 1; + } + return k = k * j; } /* ======= TESTS - DO NOT MODIFY ===== */ From 54ba3a740727c0a920df939024fdc9096aa7e63f Mon Sep 17 00:00:00 2001 From: AfshaHossain Date: Mon, 27 Mar 2023 04:27:18 +0100 Subject: [PATCH 3/3] Completed the extra part Completed the extra part on my own after writing the pseudocode and watching Barath's function and looping videos. --- extra/1-factorial.js | 25 +++++++++++++++---------- mandatory/1-fix-functions.js | 5 ++--- mandatory/2-function-creation.js | 4 +--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/extra/1-factorial.js b/extra/1-factorial.js index 2b1807b5..0ad038ea 100644 --- a/extra/1-factorial.js +++ b/extra/1-factorial.js @@ -8,20 +8,25 @@ Using a loop, complete the function below so it returns the factorial of the number being passed in. */ +/* +Pseudocode: +- We take a number and pass it as an argument into the function +- Inside the fuction the number is then multiplied to one less than the number until it reaches 1. + +*/ + function factorial(input) { - // TODO - let j = i + 1; - let k = 1; - - for (i = 0; i <= input; i++) { - while (j <= input) { - return j; - } - j = j + 1; + let multiple = 1; + for (let i = input; i > 0; i--) { + multiple = multiple * input; + input = input - 1; } - return k = k * j; + return multiple; } + + + /* ======= TESTS - DO NOT MODIFY ===== */ describe("factorial", () => { diff --git a/mandatory/1-fix-functions.js b/mandatory/1-fix-functions.js index 2f717941..db45cd7b 100644 --- a/mandatory/1-fix-functions.js +++ b/mandatory/1-fix-functions.js @@ -11,7 +11,6 @@ */ function getMood(isHappy) { - if (isHappy) { return "I am happy"; } else { @@ -20,8 +19,8 @@ function getMood(isHappy) { } function greaterThan10(num) { - - if (num > 10) { + let isBigEnough = num > 10; + if (isBigEnough) { 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 f8f648b7..94b1fee0 100644 --- a/mandatory/2-function-creation.js +++ b/mandatory/2-function-creation.js @@ -69,10 +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"; } else { - return false; + return "You Don't Need To Register"; } }