From 9b4014f86d8259b082c81e02d9ed7ece4cc84e9b Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 7 Nov 2023 18:23:54 +0000 Subject: [PATCH 01/46] week1 two errors corrected --- week-1/errors/0.js | 6 ++++-- week-1/errors/1.js | 5 +++++ week-1/errors/2.js | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..17ed8a75 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,2 +1,4 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? + +//answer. we comment the lines out like thiss \ No newline at end of file diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..1c76674c 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -2,3 +2,8 @@ const age = 33; age = age + 1; +// you cannot re-assign const + +let age = 33; +age = age + 1; + diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..31aa04f8 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -3,3 +3,8 @@ console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; + +// we should declare cityOfBirth before console.logging + +const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); From db779dcb7338185ffafd60f304c9418fbb5be57d Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 7 Nov 2023 18:36:03 +0000 Subject: [PATCH 02/46] card number fixed --- week-1/errors/3.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..1e300404 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,6 +1,11 @@ const cardNumber = 4533787178994213; const last4Digits = cardNumber.slice(-4); +//the card number must have a string format if we want to use the slice method + +const cardNumber = "4533787178994213"; +const last4Digits = cardNumber.slice(-4); + // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working // Make and explain a prediction about why the code won't work From 734380d53f34e38b6d6ac0290b96e4f3d14f1ca9 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 7 Nov 2023 18:43:32 +0000 Subject: [PATCH 03/46] clock time fixed --- week-1/errors/4.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..66b2fbdb 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,7 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; +// variables must start with letters + +const twelveHourClockTime = "20:53"; +const twentyFourhourClockTime = "08:53"; + From 0bd33c2e49f9b505a104ac0c1a74b2db108721b9 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 7 Nov 2023 18:46:17 +0000 Subject: [PATCH 04/46] commented out --- week-1/errors/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/errors/0.js b/week-1/errors/0.js index 17ed8a75..85e52911 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,4 +1,4 @@ //This is just an instruction for the first activity - but it is just for human consumption //We don't want the computer to run these 2 lines - how can we solve this problem? -//answer. we comment the lines out like thiss \ No newline at end of file +//answer. we comment the lines out like this \ No newline at end of file From c05e34c706a1adf4cff1f7abcd271f85273e4877 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 7 Nov 2023 18:55:09 +0000 Subject: [PATCH 05/46] count completed --- week-1/exercises/count.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..cbe26fc0 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -1,6 +1,11 @@ let count = 0; -count = count + 1; +count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing + +count = count + 1; //line three is increasing 0 by 1 the = is the operator here + + + From b5a6fbc4d0324aba871136ba8aa4492e6d3b43e0 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 10:37:37 +0000 Subject: [PATCH 06/46] whole number assigned --- week-1/exercises/decimal.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..1e5256c3 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -3,8 +3,6 @@ const num = 56.5467; // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math -// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) -// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5467 ( the decimal part of num ) -// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) +// Create a variable called wholeNumberPart and assign the whole number part of num to it +const wholeNumberPart = Math.floor(num); -// Log your variables to the console to check your answers From 5ffdda30e8df1b2cc8ac4a781135905347daa26d Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 10:51:36 +0000 Subject: [PATCH 07/46] decimals updated --- week-1/exercises/decimal.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index 1e5256c3..0e73d316 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -6,3 +6,13 @@ const num = 56.5467; // Create a variable called wholeNumberPart and assign the whole number part of num to it const wholeNumberPart = Math.floor(num); +// Create a variable called decimalPart and assign the decimal part of num to it +const decimalPart = num - wholeNumberPart; + +// Create a variable called roundedNum and assign num rounded to the nearest whole number to it +const roundedNum = Math.round(num); + +console.log("Whole Number Part:", wholeNumberPart); +console.log("Decimal Part:", decimalPart); +console.log("Rounded Number:", roundedNum); + From aa5e050bd1e76599f15f2949e8aaed0bf49885ad Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 11:03:47 +0000 Subject: [PATCH 08/46] initials updated --- .DS_Store | Bin 0 -> 6148 bytes week-1/exercises/count.js | 2 +- week-1/exercises/initials.js | 2 ++ 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..81ab5e0a76374b7298d84ee8f3eec06b7ab2ba02 GIT binary patch literal 6148 zcmeHK%Wl&^6upy%)}}&a0V*3LOKd|)TdESVF`<>{3aPG|1t4SBR4etljFSCa9LKqc5h=HcSMc@U z%8-+FZ|E%zDWpSSNC&h>N3@T0xRC#Sj_+dBlp;znR=SA69?vnBsYGN-r}UIInzLhH z{}vmU<8!l;AMtsfjf-CICu=RW?<}u4E6%F(A^0T9poogeco2==$3V;-f4r z!_I@(f)`Q7(~%-%Ns5s7!;B}Q9Eb@|#)=#02FG>Wu(LLuZuPgj-prMNcy{^}P z(VNX&=i!rQFAu`g{EUmwm_CeQJ5_ei;3a%Prm_5sV8nC5Pf#y8^|sA9X?~%-{^07d zTg}*(?C-kR=gnC4LVT;e5TIJS4GNf-%vrq+leKfS3RnehQ~|j^cyNin!AhgrI*_O< z0I-c}Wys5)1j+FX`UWeFXn`?J1!^iYR}7}BP+CgPD<;xuGyMI{Noi zI5A(NO|1e}fu;gWy4aED|K9cY|0c Date: Wed, 8 Nov 2023 11:22:41 +0000 Subject: [PATCH 09/46] path.js updated --- week-1/exercises/paths.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..df9d0441 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -16,3 +16,34 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable +//...................................................... + +const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; + +// The index of the last slash +const lastSlashIndex = filePath.lastIndexOf("/"); + +// The "base" part from the filePath +const base = filePath.slice(lastSlashIndex + 1); + +// Logging the "base" part +console.log(`The base part of ${filePath} is ${base}`); + +//............................................................. + +// Extracting the "dir" part from the filePath +const dir = filePath.slice(0, lastSlashIndex); + +// The console-log +console.log(`The dir part of ${filePath} is ${dir}`); +//............................................................. + +// Extracting the "ext" part from the "base" part +const extIndex = base.lastIndexOf("."); +const ext = extIndex !== -1 ? base.slice(extIndex + 1) : "No extension"; + +// the console log +console.log(`The ext part of ${filePath} is ${ext}`); +//...................................................... + + From 86cdee4838b5fb3af3865198739af3ad7e3c5230 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 11:29:39 +0000 Subject: [PATCH 10/46] random .js updated --- week-1/exercises/random.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..088a8e2d 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -7,3 +7,8 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num several times to build an idea of what the program is doing + +//Answer + +// The variable num wil contain a random number between 1 and 100. + From 197fc8910d126e1d235a977924f206cee95861d9 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 13:09:38 +0000 Subject: [PATCH 11/46] commented answers --- week-1/errors/1.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/week-1/errors/1.js b/week-1/errors/1.js index 1c76674c..997fc22e 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -2,8 +2,8 @@ const age = 33; age = age + 1; -// you cannot re-assign const +// you cannot re-assign const instead we can use: let. -let age = 33; -age = age + 1; +//let age = 33; +//age = age + 1; From 16abd137800010f8159749a88dc59eae32be5d29 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 15:02:58 +0000 Subject: [PATCH 12/46] objects updated --- week-1/explore/chrome.md | 6 ++++++ week-1/explore/objects.md | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/week-1/explore/chrome.md b/week-1/explore/chrome.md index e7dd5fea..d5450c6b 100644 --- a/week-1/explore/chrome.md +++ b/week-1/explore/chrome.md @@ -10,9 +10,15 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; + What effect does calling the `alert` function have? +// was unable to do this. it only worked when I used console.log. Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. + What effect does calling the `prompt` function have? What is the return value of `prompt`? +.......................... + +// I was only able to use console.log \ No newline at end of file diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 0216dee5..1d535000 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -5,8 +5,10 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +`console.log` Now enter just `console` in the Console, what output do you get back? +`console` Try also entering `typeof console` @@ -14,3 +16,9 @@ Answer the following questions: What does `console` store? What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +The console.log() method prints a message to the web console.  + +The console.assert() method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. + +`.` means execute this”. \ No newline at end of file From 116e4626b85754ae9b0df5a29b4da7dab51147b5 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 15:03:32 +0000 Subject: [PATCH 13/46] commented --- week-1/exercises/random.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 088a8e2d..189dcf5f 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/random.js @@ -10,5 +10,5 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; //Answer -// The variable num wil contain a random number between 1 and 100. +// The variable -num- wil contain a random number between 1 and 100. From a71b912e61f1ddebdad6b4f97a5a38190000a681 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 15:22:31 +0000 Subject: [PATCH 14/46] percentage-solved --- week-1/interpret/percentage-change.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..03ef88e2 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -12,9 +12,13 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +// ...........there are 2 function calls: 1) carPrice.replaceAll and 2) priceAfterOneYear.replaceAll // b) Identify all the lines that are variable reassignment statements +//.......1) carPrice = Number(carPrice.replaceAll(",", "")); and 2) priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); // c) Identify all the lines that are variable declarations +//.....1. `let carPrice = "10,000";` 2. `let priceAfterOneYear = "8,543";` 3. `const priceDifference = carPrice - priceAfterOneYear;` 4. `const percentageChange = (priceDifference / carPrice) * 100;` // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +//..... it is replacing all the comas in the string so that we can have numbers that we can calculate. From 0c8964fba75321a109ce5b84a0d1e8be2759a2a8 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 8 Nov 2023 18:44:23 +0000 Subject: [PATCH 15/46] to-pounds js completed --- week-1/interpret/time-format.js | 12 +++++++----- week-1/interpret/to-pounds.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 7961fe0d..83ebf4a5 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -13,17 +13,19 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions -// a) How many variable declarations are there in this program? +// a) How many variable declarations are there in this program? There are five variable declarations in this program: -// b) How many function calls are there? +// b) How many function calls are there? There are zero function calls -// c) Using documentation on MDN, explain what the expression movieLength % 60 represents +// c) Using documentation on MDN, explain what the expression movieLength % 60 represents. it represents the reminder. +// (It calculates the remaining seconds after all the full minutes have been counted.) -// d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? It extracts the seconds in the movie length. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// the variable result represent (hours:minutes:seconds) time format, a better name would be (formattedTime) // f) Think about whether this program will work for all values of movieLength. // Think of what values may cause problems for the code. // Decide the result should be for those values, then test it out. -// Can you find any values of movieLength which don't give you what you'd expect? +// Can you find any values of movieLength which don't give you what you'd expect? yes, it is not considering (days) in the calculation. diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..f3c37a85 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -27,3 +27,18 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); +// : This line creates a new string variable +// which represents the price in pence without the 'p' character. +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +// : This line creates a new string variable with the value "399," +// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +// : This line takes the first character of the `paddedPenceNumberString`, +// which represents the pounds. +// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); +// : This line extracts the pence part of the `paddedPenceNumberString` +// 6. console.log(`£${pounds}.${pence}`); +// : This line combines the `pounds` and `pence` variables with the '£' symbol to to make it easy to read. + + + From d872c9d7c9823ab04344e9ad444bc94d7f40200b Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Sat, 11 Nov 2023 15:38:27 +0000 Subject: [PATCH 16/46] NW6 | FATHI_KAHIN | JS1 | week 2 exercises | WEEK2 --- week-1/.DS_Store | Bin 0 -> 6148 bytes week-2/debug/0.js | 11 +++++++++++ week-2/debug/1.js | 13 +++++++++++-- week-2/debug/2.js | 23 +++++++++++++++++------ week-2/errors/0.js | 3 +-- 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 week-1/.DS_Store diff --git a/week-1/.DS_Store b/week-1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4ed35a8e4badd356f3a7efeee80dbe8e4cfa3723 GIT binary patch literal 6148 zcmeHK%}T>S5Z-NTyQK&{D0*D*S}-b7i84!cC#OJe;8xjn@oUd!C?ZFrLeO0qs@Y^juE$7O7A&%95UZeK zpugzCZ*Q`YrSMarU%&r7n8r~ybDR%et2Z{9EzuGkapym(g+Ck5=9xE6Z?Jc%R2&rc zIJkO2SkZM3WFwZm*+cq86T-CzDLq`X(USqCM*FESE=vn({FpizMc)V~uJNTteXWTVNEiph0lo_a-ZWqu0bNI_t zKJv>Y)FTFnfq%vTZw&pR2aB?2>$m0MSt~*BK~XR-M*{@x(j@>oxR31WpovSgA Date: Sat, 11 Nov 2023 16:15:56 +0000 Subject: [PATCH 17/46] capitalized str --- week-2/errors/0.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/week-2/errors/0.js b/week-2/errors/0.js index aeee24e8..229d3bf9 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -2,7 +2,24 @@ // write down the error you predict will be raised // then call the function capitalise with a string input // interpret the error message and figure out why it's happening, if your prediction was wrong +// function capitalise(str) { +// let str = `${str[0].toUpperCase()}${str.slice(1)}`; +// return str; +// } + +//.................................... +// the problem is that (str) is already the input to the function + function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + let capitalizedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalizedStr; } + +const originalString = "i love cyf"; +const capitalizedString = capitalise(originalString); + +console.log(`Original: ${originalString}`); +console.log(`Capitalized: ${capitalizedString}`); + + + From 49bb69d97ac7ef67dadee7d20d11f2ba796d7ce7 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Tue, 14 Nov 2023 18:26:21 +0000 Subject: [PATCH 18/46] debur and aerrors udated --- week-2/debug/2.js | 4 ++-- week-2/errors/0.js | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/week-2/debug/2.js b/week-2/debug/2.js index c1f879f6..10a86448 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -2,8 +2,8 @@ // const num = 103; -// function getLastDigit() { -// return num.toString().slice(-1); } +function getLastDigit() { +return num.toString().slice(-1); } // console.log(`The last digit of 42 is ${getLastDigit(42)}`); // console.log(`The last digit of 105 is ${getLastDigit(105)}`); diff --git a/week-2/errors/0.js b/week-2/errors/0.js index 229d3bf9..d393a1c6 100644 --- a/week-2/errors/0.js +++ b/week-2/errors/0.js @@ -2,10 +2,11 @@ // write down the error you predict will be raised // then call the function capitalise with a string input // interpret the error message and figure out why it's happening, if your prediction was wrong -// function capitalise(str) { -// let str = `${str[0].toUpperCase()}${str.slice(1)}`; -// return str; -// } + +function capitalise(str) { + let str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} //.................................... // the problem is that (str) is already the input to the function @@ -23,3 +24,4 @@ console.log(`Capitalized: ${capitalizedString}`); + From e8f119b34be7dbd5c50ab62db27983e3bf5579ba Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 10:40:34 +0000 Subject: [PATCH 19/46] 1.js completed --- week-2/errors/1.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/week-2/errors/1.js b/week-2/errors/1.js index 14b5b511..3d315a98 100644 --- a/week-2/errors/1.js +++ b/week-2/errors/1.js @@ -9,5 +9,36 @@ function convertToPercentage(decimalNumber) { return percentage; } +//................................. + +// ANSWER. +// We want the computer to perform a task: take the (decimalNumber ),, multiply it by 100, and add a '%' sign. +// However, we made a mistake by telling the computer "Hey computer, our special number is 'decimalNumber,' and by the way, it's 0.5!" +// This will confuse the computer and will result an error. To fix this, we need to correct our instructions, +// making sure that we only tell the computer about the (decimalNumber ) just once. If we solve this mistake, +// the computer will be able to carried out the task, (this is really a good example of how important is to give clear instructions, +// and how important is to avoid repetition in computer programming). + +//Playing computer results: + +// const decimalNumber is declared in the local scope +// const decimalNumber = 0.5; +// A syntax error occurs here because you're trying to redeclare 'decimalNumber' in the same scope. + +// const percentage = `${decimalNumber * 100}%`; +// This will not be reached due to the syntax error. + +// then we Called the function +// convertToPercentage(); +// This also results in a syntax error because we are trying to redeclare 'decimalNumber' in the same scope. + +function convertToPercentage() { + const decimalNumber = 0.5; + const percentage = `${decimalNumber * 100}%`; + + return percentage; +} + + +console.log(convertToPercentage()); -console.log(decimalNumber); From 2fa1a6f4d65e25e0c77f695ffba7de3cb5303d46 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 11:05:51 +0000 Subject: [PATCH 20/46] 2.js completed --- week-2/errors/2.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/week-2/errors/2.js b/week-2/errors/2.js index b0454133..37828014 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -5,6 +5,16 @@ function square(3) { return num * num; -} + } +//ANSWER + + +function square(num) { + return num * num; + } + +console.log(square(4)); +console.log(square(7)); + From 04f42c42bb101bcac2315a5545cd7b78ddf285b8 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 11:12:37 +0000 Subject: [PATCH 21/46] 2.js saved --- week-2/errors/2.js | 1 + 1 file changed, 1 insertion(+) diff --git a/week-2/errors/2.js b/week-2/errors/2.js index 37828014..1e449f86 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -7,6 +7,7 @@ function square(3) { return num * num; } +//........................................... //ANSWER From 4fd105ed235f2c888baf33bb052ed72eeecd8bf9 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 18:21:14 +0000 Subject: [PATCH 22/46] answered to-pounds.js --- week-2/implement/bmi.js | 24 ++++++++++++++++++++++++ week-2/implement/cases.js | 19 +++++++++++++++++++ week-2/implement/to-pounds.js | 23 +++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/week-2/implement/bmi.js b/week-2/implement/bmi.js index 172c7c9a..7edeb160 100644 --- a/week-2/implement/bmi.js +++ b/week-2/implement/bmi.js @@ -13,3 +13,27 @@ // Given someone's weight in kg and height in metres // When we call this function with the weight and height // Then it returns their Body Mass Index to 1 decimal place + +//.......................................... + +//ANSWERS + +function calculateBMI(weight, height) { + if (weight <= 0 || height <= 0) { + return "Invalid input. Please enter positive numbers for weight and height."; + } + + const heightSquared = height * height; + const bmi = weight / heightSquared; + + return Math.round(bmi * 10) / 10; + } + + const weight = 67; // in kg + const height = 1.73; // in meters + + const result = calculateBMI(weight, height); + console.log("BMI:", result); + + + diff --git a/week-2/implement/cases.js b/week-2/implement/cases.js index 56b0d8d0..7397d641 100644 --- a/week-2/implement/cases.js +++ b/week-2/implement/cases.js @@ -15,3 +15,22 @@ // Come up with a clear, simple name for the function // Use the string documentation to help you plan your solution + +//.......................................... + +// ANSWER + +function convertToUpperCaseWithUnderscores(inputString) { + + const words = inputString.split(" "); + + const upperCamelCaseString = words.map(word => word.toUpperCase()).join("_"); + + return upperCamelCaseString; + } + + // Test cases + console.log(convertToUpperCaseWithUnderscores("lord of the rings")); + console.log(convertToUpperCaseWithUnderscores("the great gatsby")); + console.log(convertToUpperCaseWithUnderscores("the da vinci code")); + diff --git a/week-2/implement/to-pounds.js b/week-2/implement/to-pounds.js index 7add3d05..17e744a3 100644 --- a/week-2/implement/to-pounds.js +++ b/week-2/implement/to-pounds.js @@ -3,3 +3,26 @@ // Take this code and turn it into a reusable block of code. // Declare a function called toPounds with an appropriately named parameter. // Call this function a number of times to check it works for different inputs +//..................................... + + +//ANSWER + +function toPounds(dollars) { + const exchangeRate = 0.73; + const pounds = dollars * exchangeRate; + return pounds; + } + + const amount1 = 50; + const amount2 = 100; + const amount3 = 50; + const amount4 = 100; + + console.log(`$${amount1} is £${toPounds(amount1).toFixed(2)}`); + console.log(`$${amount2} is £${toPounds(amount2).toFixed(2)}`); + console.log(`$${amount3} is £${toPounds(amount2).toFixed(2)}`); + console.log(`$${amount4} is £${toPounds(amount2).toFixed(2)}`); + + + \ No newline at end of file From 8942482f3daf6a2cb451601083f437f1a80d2f77 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 18:46:49 +0000 Subject: [PATCH 23/46] vat.js answered --- week-2/implement/vat.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index 44c38d74..a2436837 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -8,3 +8,17 @@ // Given a number, // When I call this function with a number // Then it returns the new price with VAT added on +//......................................................... + +function calculatePriceWithVAT(originalPrice) { + const vatRate = 1.2; // 20% VAT rate + const priceWithVAT = originalPrice * vatRate; + return priceWithVAT; + } + + const originalPrice = 50; + const newPrice = calculatePriceWithVAT(originalPrice); + + console.log(`Original Price: £${originalPrice}`); + console.log(`New Price with VAT: £${newPrice.toFixed(2)}`); + \ No newline at end of file From 3dc7cc7204324fa5b0c84f007f774a08c91558e6 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 18:48:32 +0000 Subject: [PATCH 24/46] 2.js commented out --- week-2/errors/2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/week-2/errors/2.js b/week-2/errors/2.js index 1e449f86..0d720876 100644 --- a/week-2/errors/2.js +++ b/week-2/errors/2.js @@ -3,9 +3,9 @@ // this function should square any number but instead we're going to get an error // what is happening? How can we fix it? -function square(3) { - return num * num; - } +// function square(3) { +// return num * num; +// } //........................................... From 718fde962eae72ec598b1c2e45e8e6d9c0a46fd9 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 19:14:35 +0000 Subject: [PATCH 25/46] Time-Format answered --- week-2/interpret/time-format.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 15793e20..147e1b72 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -41,3 +41,21 @@ console.log(formatTimeDisplay(143)); // f) Research an alternative way of padding the numbers in this code. // Look up the string functions on mdn +//.......................................................................... + +ANSWER + +// a) When we use `formatTimeDisplay`, we also use a helper called `pad` three times. We use it for hours, minutes, and seconds to make them look neat. + +// b) The first time we use `pad`, it helps us with the hours. It takes the total hours and divides them by 24 to get a special kind of hours. + +// c) The result we get from the first use of `pad` is a string that represents the hours, but it has zeros added in front to make it look good in our time display. + +// d) The last time we use `pad` in this program, it helps with the seconds. We give it the number of seconds, and it adds zeros in front to make it look nice. + +// e) The final result we get from the last use of `pad` is a string representing the seconds, but it's formatted with zeros in front to fit well in our time display. + +// f) There's another method called `String.prototype.padStart()`. It's like another tool we can use to make our numbers look neat. Here's an example: + + +// function pad(num) { return String(num).padStart(2, '0');} \ No newline at end of file From f6534519f4c092f17a56c72bed8182b68f699420 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 15 Nov 2023 19:18:17 +0000 Subject: [PATCH 26/46] time-format updated --- week-2/interpret/time-format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-2/interpret/time-format.js b/week-2/interpret/time-format.js index 147e1b72..49bba82c 100644 --- a/week-2/interpret/time-format.js +++ b/week-2/interpret/time-format.js @@ -58,4 +58,4 @@ ANSWER // f) There's another method called `String.prototype.padStart()`. It's like another tool we can use to make our numbers look neat. Here's an example: -// function pad(num) { return String(num).padStart(2, '0');} \ No newline at end of file +// function pad(num) { return String(num).padStart(2, '0');}clear From beb275f3f85fd4dde689762355ce15528e3bf70b Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Fri, 17 Nov 2023 11:15:11 +0000 Subject: [PATCH 27/46] title commentedout --- week-2/implement/vat.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/week-2/implement/vat.js b/week-2/implement/vat.js index a2436837..41977da9 100644 --- a/week-2/implement/vat.js +++ b/week-2/implement/vat.js @@ -10,6 +10,8 @@ // Then it returns the new price with VAT added on //......................................................... +// ANSWER + function calculatePriceWithVAT(originalPrice) { const vatRate = 1.2; // 20% VAT rate const priceWithVAT = originalPrice * vatRate; From d975b556ce344d1381e8132be43db123779e2247 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 09:59:31 +0000 Subject: [PATCH 28/46] format-as-12-hours.js completed! --- week-3/debug/format-as-12-hours.js | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..8bac51e9 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -28,3 +28,52 @@ console.assert( // a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" // b) Check the assertion output and explain what the bug is // c) Now fix the bug and re-run all your assertions + +//.......................................................... + +//ANSWER + +// a) Writing the assertion: +const currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "5:42 pm"; +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + + +// b) The bug is: when the hour is greater than 12 it gives a wrong answer. +// for example When the input time is "17:42," it takes away 12 from the hour and returns "5:00 pm" +// instead of "5:42 pm." + +// c) fixing the bug + + +// First wee need to format time in 12-hour clock +function formatAs12HourClock(time) { + // second we need to Extract hour and minute from the time + const hour = Number(time.slice(0, 2)); + const minute = time.slice(3); + + // Then we need to check if the hour is greater than 12 + if (hour > 12) { + // in that case we will take away 12 from the hour and add "pm" + return `${hour - 12}:${minute} pm`; + } else { + // otherwise we will keep the hour as is and add "am" + return `${hour}:${minute} am`; + } +} + + + + + + + + + + + From f5f8377035c2c285917b5195d147545f0b1ee04b Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 10:01:21 +0000 Subject: [PATCH 29/46] format-as-12-hours.js --- week-3/debug/format-as-12-hours.js | 1 + 1 file changed, 1 insertion(+) diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 8bac51e9..56121060 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -77,3 +77,4 @@ function formatAs12HourClock(time) { + From 3dc1d86d9e0a3faa3952a478b436cd9a626b6250 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 10:10:40 +0000 Subject: [PATCH 30/46] get-angle-type.js updated --- week-3/implement/get-angle-type.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..54b118fd 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,28 @@ // Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" + +// Function to identify the type of angle + + +function getAngleType(angle) { + if (angle === 90) { + return "Right angle"; + } + else if (angle < 90) { + return "Acute angle"; + } + else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + else if (angle === 180) { + return "Straight angle"; + } + else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + else { + return "Unknown angle"; + } + } + \ No newline at end of file From 1a4da93bc5d937caa6aede91b72a5fe3f2e38f61 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 10:28:38 +0000 Subject: [PATCH 31/46] get-card-value.js updated --- week-3/implement/get-card-value.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..07caa70b 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,32 @@ // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." +//........................................................... + +//ANSWER + + +function getCardValue(card) { + // first we need to extract the rank and leave the name + const rank = card.slice(0, -1); + // second we need to check if the card is a number card (2-10) + if (/^[2-9]|10$/.test(rank)) { + // Return the number value of the card + return parseInt(rank); + } + // next we need to check if the card is (J, Q, K) + else if (rank === "J" || rank === "Q" || rank === "K") { + // these cards are worth 10 points + return 10; + } + // then we check if the card is an Ace (A) + else if (rank === "A") { + // Aces are worth 11 points + return 11; + } + // after that If the card is not recognized, show an error + else { + // Show an error message for cards we don't understand + throw new Error("Invalid caRd."); + } + } From 3f51bfb294b0d09b53c078868a49ac542d488c4e Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 13:40:56 +0000 Subject: [PATCH 32/46] get-card-value.js updated --- week-3/implement/get-card-value.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 07caa70b..c34d13f4 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -55,6 +55,6 @@ function getCardValue(card) { // after that If the card is not recognized, show an error else { // Show an error message for cards we don't understand - throw new Error("Invalid caRd."); + throw new Error("Invalid card."); } } From 06822f6649ec8be3ba08ccafbc346aa3339c3a32 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 13:41:59 +0000 Subject: [PATCH 33/46] get-card-value.js update --- week-3/implement/get-angle-type.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 54b118fd..8b4d0dcd 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -23,6 +23,9 @@ // Then the function should return "Reflex angle" // Function to identify the type of angle +//............................................... + +//Answer function getAngleType(angle) { @@ -45,4 +48,3 @@ function getAngleType(angle) { return "Unknown angle"; } } - \ No newline at end of file From 9a62aeeae2542aa0613daab01cc26c4f42a06dc8 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 14:02:01 +0000 Subject: [PATCH 34/46] node is-proper-fraction.js tested --- week-3/implement/is-proper-fraction.js | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..5d05ca3a 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,41 @@ // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. // These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. + +///............................................... + + + +function isProperFraction(numerator, denominator) { + if (denominator === 0) { + return undefined; + } + if (numerator < denominator && numerator >= 0) { + return true; + } else { + return false; + } + } + + // Test cases for each acceptance criterion + +// // Proper Fraction check + const testProperFraction = isProperFraction(2, 3); + console.assert(testProperFraction === true); + +// // Improper Fraction check + const testImproperFraction = isProperFraction(5, 2); + console.assert(testImproperFraction === false); + + // Zero Denominator check + const testZeroDenominator = isProperFraction(3, 0); + console.assert(testZeroDenominator === undefined); + + // Negative Fraction check + const testNegativeFraction = isProperFraction(-4, 7); + console.assert(testNegativeFraction === true); + + // Equal Numerator and Denominator check + const testEqualNumeratorDenominator = isProperFraction(3, 3); + console.assert(testEqualNumeratorDenominator === false); + \ No newline at end of file From e158922aeb8a39b639d550c691605bef7844434b Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 14:29:05 +0000 Subject: [PATCH 35/46] is-valid-triangle.js updated --- week-3/implement/is-valid-triangle.js | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..b6408630 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,35 @@ // Then it should return true because the input forms a valid triangle. // This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. +//.................................................................... +//Answer + +// we start with a function to check if three numbers can form a triangle +function isValidTriangle(a, b, c) { + // here we are saying that sides should be greater than zero + if (a <= 0 || b <= 0 || c <= 0) { + return false; + } + + // then we need to check if the sum of any two sides is greater than the length of the third side for all possible combinations of sides + if (a + b > c && a + c > b && b + c > a) { + return true; // this means it is a valid triangle + } else { + return false; // It's not a valid triangle + } + } + + // Test examples + + // Test for (Invalid )Triangle + const testInvalidTriangle = isValidTriangle(2, 3, 6); + console.assert(testInvalidTriangle === false); + + // Test for Invalid Input: A side is less than or equal to zero + const testInvalidInput = isValidTriangle(0, 4, 5); + console.assert(testInvalidInput === false); + + // Test for a Valid Triangle + const testValidTriangle = isValidTriangle(3, 4, 5); + console.assert(testValidTriangle === true); + \ No newline at end of file From f6e2ad014ee054695da53cb970b99f1d1f845954 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 16:57:04 +0000 Subject: [PATCH 36/46] node format-as-12-hours.js updated --- week-3/refactor/format-as-12-hours.js | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..0e63c9e1 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -4,3 +4,34 @@ // Store this expression in a variable and reference it twice in the function in the correct place // Explain why it makes more sense to store this expression in a variable +//........................................ + + +//Answer +function convertTo12HourFormat(time) { + // first we need to get the number of hours from the time + const hours = Number(time.slice(0, 2)); + + // If it's after noon (more than 12 o'clock), we will adjust the time format + if (hours > 12) { + return `${hours - 12}:00 PM`; + } + + // If it's before noon, we will keep the time format as it is + return `${time} AM`; + } + + //Examples: + +// // Example 1: Converting "08:00" to 12-hour format + const time1 = convertTo12HourFormat("08:00"); + const expectedTime1 = "08:00 AM"; + console.assert( + time1 === expectedTime1); + + // Example 2: Converting "23:00" to 12-hour format + const time2 = convertTo12HourFormat("23:00"); + const expectedTime2 = "11:00 PM"; + console.assert( + time2 === expectedTime2); + From 7d9babfb053c5a81f1ddc7d51e20cb0a5953ebe5 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 17:09:18 +0000 Subject: [PATCH 37/46] is-vowel.js the exrta (i)removed --- week-3/refactor/is-vowel.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..c931340f 100644 --- a/week-3/refactor/is-vowel.js +++ b/week-3/refactor/is-vowel.js @@ -1,13 +1,27 @@ +// function isVowel(letter) { +// return ( +// letter === "a" || +// letter === "e" || +// letter === "i" || +// letter === "i" || +// letter === "o" || +// letter === "u" +// ); +// } + +//P.S. The letter (i) is included two times, that is why i commented it out +// otherwise the code is fine when on (i) is removed. + function isVowel(letter) { return ( letter === "a" || letter === "e" || letter === "i" || - letter === "i" || letter === "o" || letter === "u" ); } +localStorage // here is an implementation of isVowel - this function checks if a letter is a vowel @@ -40,3 +54,4 @@ console.assert( currentOutput3, targetOutput3 ); + From a9d634a980affd3d48a2024feb02ef5d3f6b8020 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 22 Nov 2023 17:19:47 +0000 Subject: [PATCH 38/46] is-vowel updated --- week-3/refactor/is-vowel.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index c931340f..5eff8d40 100644 --- a/week-3/refactor/is-vowel.js +++ b/week-3/refactor/is-vowel.js @@ -8,9 +8,11 @@ // letter === "u" // ); // } +//............................................... -//P.S. The letter (i) is included two times, that is why i commented it out -// otherwise the code is fine when on (i) is removed. +//ANSWER +//The letter (i) is included two times, that is why i commented it out +// otherwise the code is fine when on (i) is removed. function isVowel(letter) { return ( From 94ded21ac964bd1a1ede488db4a6683ce538d6dd Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 29 Nov 2023 18:34:56 +0000 Subject: [PATCH 39/46] count.test passed! --- week-4/implement/count.test.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/week-4/implement/count.test.js b/week-4/implement/count.test.js index 77a713a1..c590afe4 100644 --- a/week-4/implement/count.test.js +++ b/week-4/implement/count.test.js @@ -1,5 +1,7 @@ // implement a function countChar that counts the number of times a character occurs in a string +//const { default: test } = require("node:test"); + // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, // Then it should: @@ -15,3 +17,34 @@ // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +/////.................................................................................................... +//ANSWER + +// count.js +function countChar(str, char) { + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === char) { + count++; + while (str[i + 1] === char) { + i++; + count++; + } + } + } + + return count; +} + + +test('counts occurrences of a character in a string', () => { + const inputString = "aaaaaaa"; + const targetChar = "a"; + const currentOutput = countChar(inputString, targetChar); + const targetOutput = 7; + expect(currentOutput).toBe(targetOutput); +}); + + + From 30a051c3abfa653ba0a3a021373379a480d8020c Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 29 Nov 2023 19:27:39 +0000 Subject: [PATCH 40/46] get ordinal pass! --- week-4/implement/get-ordinal-number.test.js | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/week-4/implement/get-ordinal-number.test.js b/week-4/implement/get-ordinal-number.test.js index 4e735d0b..ab361f2f 100644 --- a/week-4/implement/get-ordinal-number.test.js +++ b/week-4/implement/get-ordinal-number.test.js @@ -2,3 +2,47 @@ // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback +//............................................. +//ANSWER + + + +// getOrdinalNumber(1); // returns "1st"; +// getOrdinalNumber(2); // returns "2nd"; +// getOrdinalNumber(6); // returns "6th"; + +function getOrdinalNumber(number) { + const suffix = getSuffix(number); + return `${number}${suffix}`; + } + + function getSuffix(number) { + const lastDigit = number % 10; + const lastTwoDigits = number % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return 'th'; + } + + if (lastDigit === 1) { + return 'st'; + } else if (lastDigit === 2) { + return 'nd'; + } else if (lastDigit === 3) { + return 'rd'; + } else { + return 'th'; + } + } + + +// new way of checking code using jest +test("converts 1 to an ordinal number", function () { + const input = 6; + const currentOutput = getOrdinalNumber(input); + const targetOutput = "6th"; + + expect(currentOutput).toBe(targetOutput); + }); + + From c8ef7b08947f99da3b88be4d3971efe14f33cc54 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 29 Nov 2023 19:28:04 +0000 Subject: [PATCH 41/46] is prime pass --- week-4/implement/is-prime.test.js | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/week-4/implement/is-prime.test.js b/week-4/implement/is-prime.test.js index 90199887..209a64f4 100644 --- a/week-4/implement/is-prime.test.js +++ b/week-4/implement/is-prime.test.js @@ -1,3 +1,37 @@ // Given a positive integer num, // When the isPrime function is called with num as input, // Then it should return a boolean representing whether the num is prime + +//................................................................ + +// ANSWER + +function isPrime(num) { + if (num <= 1) { + return false; // 0 and 1 are not prime numbers + } + + for (let i = 2; i <= Math.sqrt(num); i++) { + if (num % i === 0) { + return false; + } + } + + return true; + } + + test('returns true for prime number 7', () => { + expect(isPrime(7)).toBe(true); + }); + + test('returns false for non-prime number 12', () => { + expect(isPrime(12)).toBe(false); + }); + + test('returns false for 1', () => { + expect(isPrime(1)).toBe(false); + }); + + test('returns false for 0', () => { + expect(isPrime(0)).toBe(false); + }); \ No newline at end of file From 54a45a13ad1c9279fb85ff9cdfb57cb8edd314f1 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 29 Nov 2023 19:28:33 +0000 Subject: [PATCH 42/46] password validator pass --- week-4/implement/password-validator.test.js | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/week-4/implement/password-validator.test.js b/week-4/implement/password-validator.test.js index dfc7cedb..f400c3d3 100644 --- a/week-4/implement/password-validator.test.js +++ b/week-4/implement/password-validator.test.js @@ -14,3 +14,36 @@ To be valid, a password must: You must breakdown this problem in order to solve it. Find one test case first and get that working */ + + + +function isPasswordValid(password, previousPasswords = []) { + if (password.length < 5) { + return false; + } + if (!/[A-Z]/.test(password)) { + return false; + } + if (!/[a-z]/.test(password)) { + return false; + } + if (!/\d/.test(password)) { + return false; + } + if (!/[!#$%.&*]/.test(password)) { + return false; + } + if (previousPasswords.includes(password)) { + return false; + } + return true; + } + + + + test('returns true for a valid password meeting all criteria', () => { + const password = 'Secure123!'; + const previousPasswords = ['WeakPassword1!', 'InsecurePass2']; + expect(isPasswordValid(password, previousPasswords)).toBe(true); + }); + \ No newline at end of file From 0f2020cfd57048dab223212f44132ad9e9af3329 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Wed, 29 Nov 2023 19:29:22 +0000 Subject: [PATCH 43/46] repeat.test Pass --- week-4/implement/repeat.test.js | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/week-4/implement/repeat.test.js b/week-4/implement/repeat.test.js index 0b2b0a3e..ab33501c 100644 --- a/week-4/implement/repeat.test.js +++ b/week-4/implement/repeat.test.js @@ -23,3 +23,39 @@ // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +//............................................ +//Answer + +function repeat(str, count) { + if (count < 0) { + throw new Error('Negative count is not valid'); + } + + if (count === 0) { + return ''; + } + + return str.repeat(count); + } + + module.exports = repeat; + + + test('repeat String: repeats the string count times', () => { + const result = repeat('abc', 3); + expect(result).toBe('abcabcabc'); + }); + + test('handle Count of 1: returns the original string without repetition', () => { + const result = repeat('xyz', 1); + expect(result).toBe('xyz'); + }); + + test('Handle Count of 0: returns an empty string', () => { + const result = repeat('123', 0); + expect(result).toBe(''); + }); + + test('Negative Count: throws an error for negative count', () => { + expect(() => repeat('hello', -2)).toThrow('Negative count is not valid'); + }); \ No newline at end of file From 3dd33490b1a1d2a03bca5b5559af45e81c5e2db0 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Thu, 30 Nov 2023 11:26:43 +0000 Subject: [PATCH 44/46] find.js answered --- week-4/investigate/find.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/week-4/investigate/find.js b/week-4/investigate/find.js index c7e79a2f..76e3c0f5 100644 --- a/week-4/investigate/find.js +++ b/week-4/investigate/find.js @@ -23,3 +23,26 @@ console.log(find("code your future", "z")); // b) What is the if statement used to check // c) Why is index++ being used? // d) What is the condition index < str.length used for? + +//................................................................................................................ + +//ANSWER + +// We have line of letters "code your future." and we want to find a specific letter in this line, letter "u." + +// This function called "find" is like a little helper that looks at each letter in the line, one by one. It starts at the beginning and keeps going until it either finds the letter "u" or looks at every letter in the line. + +// Here's what the function called “find” which is our helper does step by step: + +// a) The helper uses a special counter, which we called "index". It starts at the beginning (the first letter), and each time it checks a letter, it moves to the next one. So, it goes from the first letter, then the second, then the third, and so on. + +// b) When the helper looks at a letter, it asks a question: "Is this letter the one we're looking for?" It uses a special rule (===) to check if the letter it's looking at is the same as the one we want to find. + +// c) The helper uses a trick called “Incrementing." to look at each letter one after the other. It does this by adding 1 to its counter, which we call “index”. + +// d) The condition “index < str.length" is used : because we don’t want our helper to go too far and look at letters that aren't there. So, it has a rule: something like: I’ll keep looking as long as I haven't looked at all the letters in the line. It checks this rule before looking at each letter. It is only after it has looked at all the letters, then it stops. + +// So, when we ask the helper to find the letter "u" in our line, it looks through each letter until it finds the first "u." If it finds it, it tells us where it found it (which position), and if it doesn't find it, it says, "I didn't find it," and tells us -1. + +// In our example, it found the first "u" at position 2 (because we start counting from 0), and it didn't find "z," so it says -1. + From a29d85f832794bd762a57ce81283b771a2897085 Mon Sep 17 00:00:00 2001 From: Fathi Kahin Date: Thu, 30 Nov 2023 11:56:55 +0000 Subject: [PATCH 45/46] updated --- .DS_Store | Bin 6148 -> 8196 bytes week-1/.DS_Store | Bin 6148 -> 6148 bytes 2 files changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 81ab5e0a76374b7298d84ee8f3eec06b7ab2ba02..7f66bce642e1c692c2e3951312ebf6c3ac91efa5 100644 GIT binary patch delta 634 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD6lbLH}hr%jz7$c**Q2SHn1=X zOy*%RU}MY!s-;LXeIq8PM z$@#ejKou~6!!x=0E-p}KacDii6!N3vs3TAdPSq)J)jouk7i19YjLD~2r6-?e;b&ut z0NNff`2mYEwxd!wdimTbVWh delta 245 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jG@dU^g=(=VTrM12zUnpp4#R zCjl!qhV@`(lYkB%qb!K|9}E~6CNC3`V`Jl7Rh3gm>vvY6=G6SW7K!6)axPn~1vG6kUtohAbtXQlVNi_&m3j| Ddp0?j diff --git a/week-1/.DS_Store b/week-1/.DS_Store index 4ed35a8e4badd356f3a7efeee80dbe8e4cfa3723..fb6ad0895a58caf1cd20eab63ec4b5b4b7ee2b47 100644 GIT binary patch delta 375 zcmZoMXfc=|#>B)qu~2NHo}wT-0|Nsi1A_nqLlHx?P7YCJee0n3?3~=Z{I1FS8RZ$fC*Nn3ujd5XQUSIpnIV&*7{~%U1j(-T$aVqQ z>|h-Q3^`zzrGlJ|P|YZdrkWFIY92!g*oFe2Pf{65kaai!1E3p23rJZaLkdGKkk$i| tDacxYI({N_Y-VJVW!cQm!OsBB`mu~2NHo}wTV0|Nsi1A_nqLlHx9Qh9N~#Dvw84MbQZ*%(rRLPZSu zFo~p`{3M{_?UO^83f07`tBoy9brj6aYjqT=EsYFx6f7+aYHK+;M3wcegW|Jua`W;# zC%<8mXY88H!z{n~3$rlGCYEoEo7p+|Ie^x0{>c2Dc{0C Date: Thu, 30 Nov 2023 12:58:52 +0000 Subject: [PATCH 46/46] card validator file passed test! --- week-4/implement/card-validator.test.js | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 week-4/implement/card-validator.test.js diff --git a/week-4/implement/card-validator.test.js b/week-4/implement/card-validator.test.js new file mode 100644 index 00000000..f056dec4 --- /dev/null +++ b/week-4/implement/card-validator.test.js @@ -0,0 +1,53 @@ + + +// This is a function that will validate the credit card number +function isCreditCardValid(cardNumber) { + // Here we are checking if the card number is a string; if not, we are converting it to a string + let cleanedCardNumber = (typeof cardNumber === 'string') ? cardNumber.replace(/[^0-9]/g, '') : ''; + + // Here we are checking if the cardNumber meets the specified criteria for validity + return ( + cleanedCardNumber.length === 16 && + hasAtLeastTwoDifferentDigits(cleanedCardNumber) && + isFinalDigitEven(cleanedCardNumber) && + isSumGreaterThan16(cleanedCardNumber) + ); +} + +// This is a helper function to check if there are at least two different digits in the number +function hasAtLeastTwoDifferentDigits(numbers) { + // then it converts the string into an array of unique numbers and check if the length is greater than or equal to 2 + return new Set(numbers.split('')).size >= 2; +} + +// The helper function then checks if the final digit is even +function isFinalDigitEven(numbers) { + // it then converts the last digit to a number and checks if it's even + return parseInt(numbers[numbers.length - 1], 10) % 2 === 0; +} + +// our helper function is here to check if the sum of all numbers is greater than 16 +function isSumGreaterThan16(numbers) { + // it Calculates the sum of all digits and checks if it's greater than 16 + return numbers.split('').reduce((sum, digit) => sum + parseInt(digit, 10), 0) > 16; +} + + + +//testing using npm test + + +test('Valid Credit Card Number', () => { + const validCardNumber = '9999777788880000'; + expect(isCreditCardValid(validCardNumber)).toBe(true); +}); + +test('Invalid Credit Card Number (Invalid Characters)', () => { + const invalidCardNumber = 'a92332119c011112'; + expect(isCreditCardValid(invalidCardNumber)).toBe(false); +}); + +test('Invalid Credit Card Number (One Type of Number)', () => { + const invalidCardNumber = '4444444444444444'; + expect(isCreditCardValid(invalidCardNumber)).toBe(false); +});