From 2dd0a787c40f47f15b8439778049e06fba3f58d6 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 17 Jun 2025 23:30:18 +0100 Subject: [PATCH 01/26] explain Increment count variable by 1 after initialization. --- Sprint-1/1-key-exercises/1-count.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..91605a33e 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -2,5 +2,6 @@ let count = 0; 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 +// Line 3 Take the current value of count, and then add 1, and store the result back in count . \ No newline at end of file From 1bc5c74bcbf970addc25aa0dc885f2d2c4527f7e Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 17 Jun 2025 23:33:40 +0100 Subject: [PATCH 02/26] Create initials variable by extracting first characters of first, middle, and last names. --- Sprint-1/1-key-exercises/2-initials.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..538997ca1 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,11 @@ let lastName = "Johnson"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; + +let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); + +console.log(initials); + // https://www.google.com/search?q=get+first+character+of+string+mdn From b16d04875b0aa045deabd421de33a78d83c63192 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 17 Jun 2025 23:36:19 +0100 Subject: [PATCH 03/26] create two variable and Extract directory and file extension parts from file path string using slice. --- Sprint-1/1-key-exercises/3-paths.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..4daa0d8d9 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,8 @@ 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 dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex); +const ext = base.slice(base.lastIndexOf(".") + 1); + // https://www.google.com/search?q=slice+mdn \ No newline at end of file From 625c1142588d533ec5988676624ed6e3f82bbfd6 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 17 Jun 2025 23:39:18 +0100 Subject: [PATCH 04/26] Add comments explaining how num is calculated as a random integer between minimum and maximum. --- Sprint-1/1-key-exercises/4-random.js | 75 ++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..04a0e0a07 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -3,7 +3,74 @@ const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; -// In this exercise, you will need to work out what num represents? -// 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 and running the program several times to build an idea of what the program is doing +// 1- In this exercise, you will need to work out what num represents? + +// the num gives a random whole number between 1 and 100 like 73, 12, or 100. + +// 2- Try breaking down the expression and using documentation to explain what it means +/* + +1. Math.random() + +Returns a random decimal number between 0 and 1 but never gives 1.0. + +Example: 0.24 + +2. (maximum - minimum + 1) + +This gives number of possible values. + +Without the +1, we'd only get the difference, not the full count. + +for example: + +5 - 1 = 4 → but there are actually 5 numbers: 1, 2, 3, 4, 5 + +So we add +1 to include both ends of the range. + +3. Math.random() * (maximum - minimum + 1) + +This gives a random decimal number between 0 and 100 (like 24, 65 ...) + +Because we want the random decimal scaled to the size of the range of possible values. + +For example, if we want a number between 1 and 100 (inclusive), there are 100 possible numbers (1, 2, ..., 100). + +Multiplying by 100 means the decimal is scaled up to cover all those possibilities before rounding. + +4. Math.floor(...) + +This rounds the decimal down to the nearest whole number. + +Example: Math.floor(78.43) → 78 + +5. + minimum + +we add the minimum to shift the range correctly, and make sure the random number up to start from minimum. + +5-1- for example if we remove the + minimum + +5-1-1 Math.random() 0.9999 * 99 + 1 → only goes up to 99.999... → max = 99.999... → floor = 100 (but very unlikely) + +now 100 becomes very hard to reach, and in many cases, you never get it. + +5-1-2 Math.random() 0.00 * 99 + 1 → only goes up to 0... → max = 0... → floor = 0 (now the minimum is 0, and can appears) + +conclusion : when we don’t add + minimum, there is a chance that 1 appears, but it’s not the guaranteed minimum anymore — + +and the range starts at 0, not 1. + +5-2- when we add +minimum + +now we make sure the min and max can appear in the final results and make sure the minimum is 1 not 0. + +Minimum appears when random = 0 + +Maximum appears when random is almost 1 (like 0.9999...). + +example : Math.random() * 99 + 1 → up to 0.99 → max = 99 → floor = 99 → +1 = 100 (so more possibilities for 100 to appears) + +*/ + +//It will help to think about the order in which expressions are evaluated +//Try logging the value of num and running the program several times to build an idea of what the program is doing From f5522e711f8c7c8e93631c8cf4bce1b964409cff Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 17 Jun 2025 23:41:09 +0100 Subject: [PATCH 05/26] Add explanation for incrementing count variable by 1 after initialization. --- Sprint-1/1-key-exercises/1-count.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 91605a33e..dfb02c25f 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,4 +4,4 @@ count = count + 1; // // Line 1 is a variable declaration, creating the count variable with an initial value of 0 -// Line 3 Take the current value of count, and then add 1, and store the result back in count . \ No newline at end of file +// Line 3 Take the current value of count, and then add 1, and store the result back in count. \ No newline at end of file From 1cf3f70276c316fbfe7b2e3d3abb4e658968079d Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 6 Jul 2025 16:09:11 +0100 Subject: [PATCH 06/26] I believe that I've Done the sprint 1 and this the last exercise. --- Sprint-1/4-stretch-explore/chrome.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..d820607c7 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -10,9 +10,17 @@ 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? +What effect does calling the `alert` function have? It opens a popup message box in the browser with the text: +You must click "OK" to dismiss it before you can do anything else on the page. 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`? +What effect does calling the `prompt` function have? It opens a popup input box asking: +What is your name? +and I can enter a response, then click "OK" or "Cancel". + +What is the return value of `prompt`? Return value: + +If I enter a name (e.g., "Sami") and press OK, it returns the string: "Sami" + +If I press Cancel, it returns: null From b76c5a3af8475052f5ba97c02a21e16b96afa26f Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 6 Jul 2025 16:10:17 +0100 Subject: [PATCH 07/26] I believe that I've completed the first sprint. --- Sprint-1/4-stretch-explore/objects.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..81d9d81f2 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -4,13 +4,18 @@ 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? +What output do you get? output: ƒ log() { [native code] } -Now enter just `console` in the Console, what output do you get back? +Now enter just `console` in the Console, what output do you get back?output: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} -Try also entering `typeof console` +Try also entering `typeof console` output : object Answer the following questions: -What does `console` store? +What does `console` store? stores a collection of methods (functions) that are used to log information or debug JavaScript code. + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +This syntax uses dot notation. In JavaScript, the dot . is used to access a property or method of an object. + +console is the object. From 0748f7c1c10cee49fec86f87e857125530314570 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 13 Jul 2025 11:30:44 +0100 Subject: [PATCH 08/26] I've changed const to let for age variable to allow reassignment --- Sprint-1/2-mandatory-errors/1.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..3e3c51959 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,5 @@ -// trying to create an age variable and then reassign the value by 1 +// Trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +let age = 33; // Declare a variable called age and assign it the value 33 +age = age + 1; // Reassign age to be its current value plus 1 +console.log(age); // Output: 34 From d957dd1b593d800996c9496518224ca81a8f3ea3 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 13 Jul 2025 15:38:37 +0100 Subject: [PATCH 09/26] Avoid redeclaring 'str' inside capitalise function --- Sprint-2/1-key-errors/0.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..3d6a25c32 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,7 @@ // Predict and explain first... -// =============> write your prediction here +// =============> // =============> write your prediction here +// I predict the code will throw an error because the variable `str` is being declared twice in the same function scope. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +12,16 @@ function capitalise(str) { } // =============> write your explanation here +// The function already receives `str` as a parameter. +// Inside the function, it tries to declare `let str = ...`, +// which causes a conflict because JavaScript does not +// allow redeclaring the same variable name in the same scope using `let`. +// This leads to a `SyntaxError: Identifier 'str' has already been declared`. + + // =============> write your new code here +// =============> write your new code here +function capitalise(str) { + let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalisedStr; +} From fcba7aa37c53c7e539da4334533613ed08957a44 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 13 Jul 2025 15:39:14 +0100 Subject: [PATCH 10/26] Remove redeclaration of parameter and undefined variable usage in convertToPercentage --- Sprint-2/1-key-errors/1.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..2301135b7 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,4 +1,9 @@ // Predict and explain first... +// =============> write your prediction here +// I predict this program will throw a `SyntaxError` because +// the parameter `decimalNumber` is being redeclared inside the function using `const`. +// Also, the variable `decimalNumber` is being accessed outside the function where it's not defined. + // Why will an error occur when this program runs? // =============> write your prediction here @@ -15,6 +20,20 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +// The function already has a parameter named `decimalNumber`, +// but inside the function, it's trying to declare another +// constant with the same name: `const decimalNumber = 0.5;`. +// JavaScript doesn’t allow redeclaring a variable that already exists in the same scope. +// Also, `console.log(decimalNumber)` is outside the function and `decimalNumber` is not +// defined in the global scope, so that will cause a `ReferenceError`. + // Finally, correct the code to fix the problem // =============> write your new code here + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(0.5)); From c278bbbc35ed7e7e4ebebc74b17f3100b9f49126 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Sun, 13 Jul 2025 15:39:39 +0100 Subject: [PATCH 11/26] Use valid parameter name instead of number in square function --- Sprint-2/1-key-errors/2.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..6bb68b409 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -2,19 +2,32 @@ // Predict and explain first BEFORE you run any code... // this function should square any number but instead we're going to get an error +// The code will throw a **SyntaxError** because you cannot +// use a number (`3`) as a function parameter name. // =============> write your prediction of the error here +// SyntaxError: Unexpected number function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// In JavaScript, function parameters must be valid variable names (identifiers). +// Using a number like `3` directly as a parameter name is invalid syntax, +// so JavaScript throws a `SyntaxError: Unexpected number`. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); + From 335cc3778604af0407eabc63d4b6f4a561fcd10e Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:09:52 +0100 Subject: [PATCH 12/26] Avoid redeclaring 'str' inside capitalise function --- Sprint-2/1-key-errors/0.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 3d6a25c32..18eac0562 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -11,7 +11,7 @@ function capitalise(str) { return str; } -// =============> write your explanation here +// =============> write your explanation here // The function already receives `str` as a parameter. // Inside the function, it tries to declare `let str = ...`, // which causes a conflict because JavaScript does not From 2f9d4dadab58f5b363f4bdfcdb3670b46eeb03d4 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:17:32 +0100 Subject: [PATCH 13/26] Return value from multiply function instead of just logging it --- Sprint-2/2-mandatory-debug/0.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..7ad604f02 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,10 @@ // Predict and explain first... // =============> write your prediction here +// I predict that the console will log `320` first (from inside the function), +// but then the message will say: "The result of multiplying 10 and 32 is undefined". + +// Explanation: function multiply(a, b) { console.log(a * b); @@ -9,6 +13,16 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function `multiply` logs the result of `a * b`, but does not return it. +// When it's used inside a template literal, JavaScript evaluates `multiply(10, 32)` as `undefined`, +// because the function has no return value. That's why we get: +// `The result of multiplying 10 and 32 is undefined` — even though `320` is logged separately. // Finally, correct the code to fix the problem // =============> write your new code here + +function multiply(a, b) { + return a * b; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); From bdc5ac9083f584fdd116167a68d1c0fc7a97c98f Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:22:00 +0100 Subject: [PATCH 14/26] Correct return statement in sum function to return a + b --- Sprint-2/2-mandatory-debug/1.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..efd11aa12 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,27 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here +// I predict that this will print: "The sum of 10 and 32 is undefined" +// because the function has a return statement with no value. + +// Explanation: function sum(a, b) { return; a + b; + // =============> write your explanation here + // The `return;` statement ends the function early, so `a + b` is never reached. + // In JavaScript, any code after a bare `return;` is ignored (unreachable code). + // That's why the function returns `undefined`, not the actual sum. } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +// Now the function returns the correct result: 42 + From 16d657745167e1574b8a7e30de2ea0264e6a8401 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:25:00 +0100 Subject: [PATCH 15/26] Update getLastDigit function to use input parameter instead of constant value --- Sprint-2/2-mandatory-debug/2.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..da217a8a5 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,7 +2,11 @@ // Predict the output of the following code: // =============> Write your prediction here +// I predict that all three log statements will output: "The last digit of 42 is 3", "105 is 3", "806 is 3" +// Because the getLastDigit function doesn’t use the input — it always returns the last digit of the constant +// `num = 103`. +// Original Code const num = 103; function getLastDigit() { @@ -15,10 +19,25 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`); // Now run the code and compare the output to your prediction // =============> write the output here +// The actual output is: +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 + // Explain why the output is the way it is // =============> write your explanation here +// The function `getLastDigit` doesn't take any parameter, and it always uses the fixed variable `num = 103`. +// So regardless of the input in the log statements, the function always returns the last digit of 103, which is "3". + // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(number) { + return number.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2 +console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5 +console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6 + +// Now the function works correctly by taking a number as input and returning its last digit. -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem From 8796b5bbdf4ade5f34f872f0b3a9916360ed2556 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:33:08 +0100 Subject: [PATCH 16/26] Implement calculateBMI function with 1 decimal rounding --- Sprint-2/3-mandatory-implement/1-bmi.js | 26 ++++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..f7a8d0d55 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -1,19 +1,13 @@ -// Below are the steps for how BMI is calculated - -// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared. - -// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by: - -// squaring your height: 1.73 x 1.73 = 2.99 -// dividing 70 by 2.99 = 23.41 -// Your result will be displayed to 1 decimal place, for example 23.4. +function calculateBMI(weight, height) { + // Square the height (height in meters) + const heightSquared = height * height; -// You will need to implement a function that calculates the BMI of someone based off their weight and height + // Divide weight by height squared to get BMI + const bmi = weight / heightSquared; -// Given someone's weight in kg and height in metres -// Then when we call this function with the weight and height -// It should return their Body Mass Index to 1 decimal place + // Return the BMI rounded to 1 decimal place + return bmi.toFixed(1); // toFixed returns a string, which is fine unless you need it as a number +} -function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height -} \ No newline at end of file +// Example usage: +console.log(calculateBMI(70, 1.73)); // Output: "23.4" From 1b5f2cf32617fb033f7f425d892371ce160b733c Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:35:28 +0100 Subject: [PATCH 17/26] Convert strings to UPPER_SNAKE_CASE format --- Sprint-2/3-mandatory-implement/2-cases.js | 25 +++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..51ac5b029 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -1,16 +1,15 @@ -// A set of words can be grouped together in different cases. +// This function converts any sentence into UPPER_SNAKE_CASE +function toUpperSnakeCase(input) { + // Step 1: Convert the string to uppercase + const upperCase = input.toUpperCase(); -// For example, "hello there" in snake case would be written "hello_there" -// UPPER_SNAKE_CASE means taking a string and writing it in all caps with underscores instead of spaces. + // Step 2: Replace all spaces with underscores + const snakeCase = upperCase.replace(/ /g, "_"); -// Implement a function that: + // Step 3: Return the result + return snakeCase; +} -// Given a string input like "hello there" -// When we call this function with the input string -// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE" - -// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS" - -// You will need to come up with an appropriate name for the function -// Use the MDN string documentation to help you find a solution -// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase +// Example usage: +console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" From 6573f60f28907801072b2ef9cc5183e9c3a9d744 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:38:11 +0100 Subject: [PATCH 18/26] Create reusable toPounds function with test cases --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..1d9d78770 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,11 @@ -// In Sprint-1, there is a program written in interpret/to-pounds.js +// Define a function that converts kilograms to pounds +function toPounds(kg) { + const pounds = kg * 2.20462; + return pounds.toFixed(2); // rounds to 2 decimal places +} -// You will need to take this code and turn it into a reusable block of code. -// You will need to declare a function called toPounds with an appropriately named parameter. - -// You should call this function a number of times to check it works for different inputs +// Call the function with different inputs to test it +console.log(toPounds(1)); // 2.20 +console.log(toPounds(5)); // 11.02 +console.log(toPounds(70)); // 154.32 +console.log(toPounds(0)); // 0.00 From 01a1cedee29b363462de755a2882358ced6a0568 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:44:46 +0100 Subject: [PATCH 19/26] Add inline comments explaining pad and formatTimeDisplay behaviour --- Sprint-2/4-mandatory-interpret/time-format.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..4db69649c 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,22 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// ===> 3 times — once for hours, once for minutes, and once for seconds. // Call formatTimeDisplay with an input of 61, now answer the following: // b) What is the value assigned to num when pad is called for the first time? -// =============> write your answer here +// ===> 0 — the first pad call is for totalHours, which is 0 when input is 61 -// c) What is the return value of pad is called for the first time? -// =============> write your answer here +// c) What is the return value of pad when called for the first time? +// ===> "00" — because pad(0) returns the string "00" // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// ===> 1 — the last pad call is for remainingSeconds, which is 61 % 60 = 1 // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer -// =============> write your answer here +// ===> "01" — because pad(1) turns 1 into "01" using padStart(2, "0") + +// Example run: +console.log(formatTimeDisplay(61)); // Output: "00:01:01" + From eeff899a77bd5d4122389260566eb9d96369e258 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 15 Jul 2025 19:48:11 +0100 Subject: [PATCH 20/26] Correct 12-hour clock formatting and add comprehensive tests --- Sprint-2/5-stretch-extend/format-time.js | 49 +++++++++++++++--------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..7c8bfeaa3 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -1,25 +1,36 @@ -// This is the latest solution to the problem from the prep. -// Make sure to do the prep before you do the coursework -// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. - function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + + if (hours === 0) { + // midnight + return `12:${minutes} am`; + } else if (hours === 12) { + // noon + return `12:${minutes} pm`; + } else if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } else { + // 1am to 11am + return `${time} am`; } - return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; -console.assert( - currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` -); +// Tests: +const tests = [ + { input: "00:00", expected: "12:00 am" }, + { input: "08:00", expected: "08:00 am" }, + { input: "12:00", expected: "12:00 pm" }, + { input: "15:30", expected: "3:30 pm" }, + { input: "23:59", expected: "11:59 pm" }, + { input: "11:15", expected: "11:15 am" }, +]; + +tests.forEach(({ input, expected }) => { + const output = formatAs12HourClock(input); + console.assert( + output === expected, + `FAIL: input=${input}, output=${output}, expected=${expected}` + ); +}); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; -console.assert( - currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` -); From f39781ab4aa7f56955328b58484c835228465815 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Mon, 21 Jul 2025 19:01:14 +0100 Subject: [PATCH 21/26] Revert Sprint-1 folder to CYF's original version --- Sprint-1/1-key-exercises/1-count.js | 7 -- Sprint-1/1-key-exercises/2-initials.js | 15 ---- Sprint-1/1-key-exercises/3-paths.js | 24 ------ Sprint-1/1-key-exercises/4-random.js | 76 ------------------- Sprint-1/2-mandatory-errors/0.js | 2 - Sprint-1/2-mandatory-errors/1.js | 5 -- Sprint-1/2-mandatory-errors/2.js | 5 -- Sprint-1/2-mandatory-errors/3.js | 9 --- Sprint-1/2-mandatory-errors/4.js | 2 - .../1-percentage-change.js | 22 ------ .../3-mandatory-interpret/2-time-format.js | 25 ------ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 27 ------- Sprint-1/4-stretch-explore/chrome.md | 26 ------- Sprint-1/4-stretch-explore/objects.md | 21 ----- Sprint-1/readme.md | 35 --------- 15 files changed, 301 deletions(-) delete mode 100644 Sprint-1/1-key-exercises/1-count.js delete mode 100644 Sprint-1/1-key-exercises/2-initials.js delete mode 100644 Sprint-1/1-key-exercises/3-paths.js delete mode 100644 Sprint-1/1-key-exercises/4-random.js delete mode 100644 Sprint-1/2-mandatory-errors/0.js delete mode 100644 Sprint-1/2-mandatory-errors/1.js delete mode 100644 Sprint-1/2-mandatory-errors/2.js delete mode 100644 Sprint-1/2-mandatory-errors/3.js delete mode 100644 Sprint-1/2-mandatory-errors/4.js delete mode 100644 Sprint-1/3-mandatory-interpret/1-percentage-change.js delete mode 100644 Sprint-1/3-mandatory-interpret/2-time-format.js delete mode 100644 Sprint-1/3-mandatory-interpret/3-to-pounds.js delete mode 100644 Sprint-1/4-stretch-explore/chrome.md delete mode 100644 Sprint-1/4-stretch-explore/objects.md delete mode 100644 Sprint-1/readme.md diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js deleted file mode 100644 index dfb02c25f..000000000 --- a/Sprint-1/1-key-exercises/1-count.js +++ /dev/null @@ -1,7 +0,0 @@ -let count = 0; - -count = count + 1; - -// -// Line 1 is a variable declaration, creating the count variable with an initial value of 0 -// Line 3 Take the current value of count, and then add 1, and store the result back in count. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js deleted file mode 100644 index 538997ca1..000000000 --- a/Sprint-1/1-key-exercises/2-initials.js +++ /dev/null @@ -1,15 +0,0 @@ -let firstName = "Creola"; -let middleName = "Katherine"; -let lastName = "Johnson"; - -// Declare a variable called initials that stores the first character of each string. -// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. - - -let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); - -console.log(initials); - - -// https://www.google.com/search?q=get+first+character+of+string+mdn - diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js deleted file mode 100644 index 4daa0d8d9..000000000 --- a/Sprint-1/1-key-exercises/3-paths.js +++ /dev/null @@ -1,24 +0,0 @@ -// The diagram below shows the different names for parts of a file path on a Unix operating system - -// ┌─────────────────────┬────────────┐ -// │ dir │ base │ -// ├──────┬ ├──────┬─────┤ -// │ root │ │ name │ ext │ -// " / home/user/dir / file .txt " -// └──────┴──────────────┴──────┴─────┘ - -// (All spaces in the "" line should be ignored. They are purely for formatting.) - -const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; -const lastSlashIndex = filePath.lastIndexOf("/"); -const base = filePath.slice(lastSlashIndex + 1); -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 dir = filePath.slice(0, lastSlashIndex); -const ext = base.slice(base.lastIndexOf(".") + 1); - - -// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js deleted file mode 100644 index 04a0e0a07..000000000 --- a/Sprint-1/1-key-exercises/4-random.js +++ /dev/null @@ -1,76 +0,0 @@ -const minimum = 1; -const maximum = 100; - -const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; - -// 1- In this exercise, you will need to work out what num represents? - -// the num gives a random whole number between 1 and 100 like 73, 12, or 100. - -// 2- Try breaking down the expression and using documentation to explain what it means -/* - -1. Math.random() - -Returns a random decimal number between 0 and 1 but never gives 1.0. - -Example: 0.24 - -2. (maximum - minimum + 1) - -This gives number of possible values. - -Without the +1, we'd only get the difference, not the full count. - -for example: - -5 - 1 = 4 → but there are actually 5 numbers: 1, 2, 3, 4, 5 - -So we add +1 to include both ends of the range. - -3. Math.random() * (maximum - minimum + 1) - -This gives a random decimal number between 0 and 100 (like 24, 65 ...) - -Because we want the random decimal scaled to the size of the range of possible values. - -For example, if we want a number between 1 and 100 (inclusive), there are 100 possible numbers (1, 2, ..., 100). - -Multiplying by 100 means the decimal is scaled up to cover all those possibilities before rounding. - -4. Math.floor(...) - -This rounds the decimal down to the nearest whole number. - -Example: Math.floor(78.43) → 78 - -5. + minimum - -we add the minimum to shift the range correctly, and make sure the random number up to start from minimum. - -5-1- for example if we remove the + minimum - -5-1-1 Math.random() 0.9999 * 99 + 1 → only goes up to 99.999... → max = 99.999... → floor = 100 (but very unlikely) - -now 100 becomes very hard to reach, and in many cases, you never get it. - -5-1-2 Math.random() 0.00 * 99 + 1 → only goes up to 0... → max = 0... → floor = 0 (now the minimum is 0, and can appears) - -conclusion : when we don’t add + minimum, there is a chance that 1 appears, but it’s not the guaranteed minimum anymore — - -and the range starts at 0, not 1. - -5-2- when we add +minimum - -now we make sure the min and max can appear in the final results and make sure the minimum is 1 not 0. - -Minimum appears when random = 0 - -Maximum appears when random is almost 1 (like 0.9999...). - -example : Math.random() * 99 + 1 → up to 0.99 → max = 99 → floor = 99 → +1 = 100 (so more possibilities for 100 to appears) - -*/ - -//It will help to think about the order in which expressions are evaluated -//Try logging the value of num and running the program several times to build an idea of what the program is doing diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js deleted file mode 100644 index cf6c5039f..000000000 --- a/Sprint-1/2-mandatory-errors/0.js +++ /dev/null @@ -1,2 +0,0 @@ -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 diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js deleted file mode 100644 index 3e3c51959..000000000 --- a/Sprint-1/2-mandatory-errors/1.js +++ /dev/null @@ -1,5 +0,0 @@ -// Trying to create an age variable and then reassign the value by 1 - -let age = 33; // Declare a variable called age and assign it the value 33 -age = age + 1; // Reassign age to be its current value plus 1 -console.log(age); // Output: 34 diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js deleted file mode 100644 index e09b89831..000000000 --- a/Sprint-1/2-mandatory-errors/2.js +++ /dev/null @@ -1,5 +0,0 @@ -// Currently trying to print the string "I was born in Bolton" but it isn't working... -// what's the error ? - -console.log(`I was born in ${cityOfBirth}`); -const cityOfBirth = "Bolton"; diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js deleted file mode 100644 index ec101884d..000000000 --- a/Sprint-1/2-mandatory-errors/3.js +++ /dev/null @@ -1,9 +0,0 @@ -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 -// Before running the code, make and explain a prediction about why the code won't work -// Then run the code and see what error it gives. -// Consider: Why does it give this error? Is this what I predicted? If not, what's different? -// Then try updating the expression last4Digits is assigned to, in order to get the correct value diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js deleted file mode 100644 index 21dad8c5d..000000000 --- a/Sprint-1/2-mandatory-errors/4.js +++ /dev/null @@ -1,2 +0,0 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js deleted file mode 100644 index e24ecb8e1..000000000 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ /dev/null @@ -1,22 +0,0 @@ -let carPrice = "10,000"; -let priceAfterOneYear = "8,543"; - -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); - -const priceDifference = carPrice - priceAfterOneYear; -const percentageChange = (priceDifference / carPrice) * 100; - -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 - -// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? - -// c) Identify all the lines that are variable reassignment statements - -// d) Identify all the lines that are variable declarations - -// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js deleted file mode 100644 index 47d239558..000000000 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ /dev/null @@ -1,25 +0,0 @@ -const movieLength = 8784; // length of movie in seconds - -const remainingSeconds = movieLength % 60; -const totalMinutes = (movieLength - remainingSeconds) / 60; - -const remainingMinutes = totalMinutes % 60; -const totalHours = (totalMinutes - remainingMinutes) / 60; - -const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; -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? - -// b) How many function calls are there? - -// c) Using documentation, explain what the expression movieLength % 60 represents -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators - -// d) Interpret line 4, what does the expression assigned to totalMinutes mean? - -// e) What do you think the variable result represents? Can you think of a better name for this variable? - -// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js deleted file mode 100644 index 60c9ace69..000000000 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ /dev/null @@ -1,27 +0,0 @@ -const penceString = "399p"; - -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); - -const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); - -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); - -console.log(`£${pounds}.${pence}`); - -// This program takes a string representing a price in pence -// The program then builds up a string representing the price in pounds - -// You need to do a step-by-step breakdown of each line in this program -// Try and describe the purpose / rationale behind each step - -// To begin, we can start with -// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md deleted file mode 100644 index d820607c7..000000000 --- a/Sprint-1/4-stretch-explore/chrome.md +++ /dev/null @@ -1,26 +0,0 @@ -Open a new window in Chrome, - -then locate the **Console** tab. - -Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). -Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. - -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? It opens a popup message box in the browser with the text: -You must click "OK" to dismiss it before you can do anything else on the page. - -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? It opens a popup input box asking: -What is your name? -and I can enter a response, then click "OK" or "Cancel". - -What is the return value of `prompt`? Return value: - -If I enter a name (e.g., "Sami") and press OK, it returns the string: "Sami" - -If I press Cancel, it returns: null diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md deleted file mode 100644 index 81d9d81f2..000000000 --- a/Sprint-1/4-stretch-explore/objects.md +++ /dev/null @@ -1,21 +0,0 @@ -## Objects - -In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. - -Open the Chrome devtools Console, type in `console.log` and then hit enter - -What output do you get? output: ƒ log() { [native code] } - -Now enter just `console` in the Console, what output do you get back?output: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} - -Try also entering `typeof console` output : object - -Answer the following questions: - -What does `console` store? stores a collection of methods (functions) that are used to log information or debug JavaScript code. - -What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? - -This syntax uses dot notation. In JavaScript, the dot . is used to access a property or method of an object. - -console is the object. diff --git a/Sprint-1/readme.md b/Sprint-1/readme.md deleted file mode 100644 index 62d24c958..000000000 --- a/Sprint-1/readme.md +++ /dev/null @@ -1,35 +0,0 @@ -# 🧭 Guide to Week 1 exercises - -> https://programming.codeyourfuture.io/structuring-data/sprints/1/prep/ - -> [!TIP] -> You should always do the prep work _before_ attempting the coursework. -> The prep shows you _how_ to do the coursework. -> There is often a step by step video you can code along with too. -> Do the prep. - -This README will guide you through the different sections for this week. - -## 1 Exercises - -In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript - -## 2 Errors - -In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. - -## 3 Interpret - -In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. - -You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! - -You can also use `console.log` to check the value of different variables in the code. - -https://developer.mozilla.org/en-US/docs/Web/JavaScript - -## 4 Explore - Stretch 💪 - -This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions. From bce599b731cdc380315e4951bc4fdcba477cd1ea Mon Sep 17 00:00:00 2001 From: Samiuk Date: Mon, 21 Jul 2025 19:23:59 +0100 Subject: [PATCH 22/26] Revert Sprint-1 folder to CYF's original version --- Sprint-1/1-key-exercises/1-count.js | 6 ++++ Sprint-1/1-key-exercises/2-initials.js | 11 ++++++ Sprint-1/1-key-exercises/3-paths.js | 23 ++++++++++++ Sprint-1/1-key-exercises/4-random.js | 9 +++++ Sprint-1/2-mandatory-errors/0.js | 2 ++ Sprint-1/2-mandatory-errors/1.js | 4 +++ Sprint-1/2-mandatory-errors/2.js | 5 +++ Sprint-1/2-mandatory-errors/3.js | 9 +++++ Sprint-1/2-mandatory-errors/4.js | 2 ++ .../1-percentage-change.js | 22 ++++++++++++ .../3-mandatory-interpret/2-time-format.js | 25 +++++++++++++ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 27 ++++++++++++++ Sprint-1/4-stretch-explore/chrome.md | 18 ++++++++++ Sprint-1/4-stretch-explore/objects.md | 16 +++++++++ Sprint-1/readme.md | 35 +++++++++++++++++++ 15 files changed, 214 insertions(+) create mode 100644 Sprint-1/1-key-exercises/1-count.js create mode 100644 Sprint-1/1-key-exercises/2-initials.js create mode 100644 Sprint-1/1-key-exercises/3-paths.js create mode 100644 Sprint-1/1-key-exercises/4-random.js create mode 100644 Sprint-1/2-mandatory-errors/0.js create mode 100644 Sprint-1/2-mandatory-errors/1.js create mode 100644 Sprint-1/2-mandatory-errors/2.js create mode 100644 Sprint-1/2-mandatory-errors/3.js create mode 100644 Sprint-1/2-mandatory-errors/4.js create mode 100644 Sprint-1/3-mandatory-interpret/1-percentage-change.js create mode 100644 Sprint-1/3-mandatory-interpret/2-time-format.js create mode 100644 Sprint-1/3-mandatory-interpret/3-to-pounds.js create mode 100644 Sprint-1/4-stretch-explore/chrome.md create mode 100644 Sprint-1/4-stretch-explore/objects.md create mode 100644 Sprint-1/readme.md diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js new file mode 100644 index 000000000..117bcb2b6 --- /dev/null +++ b/Sprint-1/1-key-exercises/1-count.js @@ -0,0 +1,6 @@ +let count = 0; + +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 diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js new file mode 100644 index 000000000..47561f617 --- /dev/null +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -0,0 +1,11 @@ +let firstName = "Creola"; +let middleName = "Katherine"; +let lastName = "Johnson"; + +// Declare a variable called initials that stores the first character of each string. +// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. + +let initials = ``; + +// https://www.google.com/search?q=get+first+character+of+string+mdn + diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js new file mode 100644 index 000000000..ab90ebb28 --- /dev/null +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -0,0 +1,23 @@ +// The diagram below shows the different names for parts of a file path on a Unix operating system + +// ┌─────────────────────┬────────────┐ +// │ dir │ base │ +// ├──────┬ ├──────┬─────┤ +// │ root │ │ name │ ext │ +// " / home/user/dir / file .txt " +// └──────┴──────────────┴──────┴─────┘ + +// (All spaces in the "" line should be ignored. They are purely for formatting.) + +const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; +const lastSlashIndex = filePath.lastIndexOf("/"); +const base = filePath.slice(lastSlashIndex + 1); +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 dir = ; +const ext = ; + +// https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js new file mode 100644 index 000000000..292f83aab --- /dev/null +++ b/Sprint-1/1-key-exercises/4-random.js @@ -0,0 +1,9 @@ +const minimum = 1; +const maximum = 100; + +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +// In this exercise, you will need to work out what num represents? +// 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 and running the program several times to build an idea of what the program is doing diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js new file mode 100644 index 000000000..cf6c5039f --- /dev/null +++ b/Sprint-1/2-mandatory-errors/0.js @@ -0,0 +1,2 @@ +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 diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js new file mode 100644 index 000000000..7a43cbea7 --- /dev/null +++ b/Sprint-1/2-mandatory-errors/1.js @@ -0,0 +1,4 @@ +// trying to create an age variable and then reassign the value by 1 + +const age = 33; +age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js new file mode 100644 index 000000000..e09b89831 --- /dev/null +++ b/Sprint-1/2-mandatory-errors/2.js @@ -0,0 +1,5 @@ +// Currently trying to print the string "I was born in Bolton" but it isn't working... +// what's the error ? + +console.log(`I was born in ${cityOfBirth}`); +const cityOfBirth = "Bolton"; diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js new file mode 100644 index 000000000..ec101884d --- /dev/null +++ b/Sprint-1/2-mandatory-errors/3.js @@ -0,0 +1,9 @@ +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 +// Before running the code, make and explain a prediction about why the code won't work +// Then run the code and see what error it gives. +// Consider: Why does it give this error? Is this what I predicted? If not, what's different? +// Then try updating the expression last4Digits is assigned to, in order to get the correct value diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js new file mode 100644 index 000000000..21dad8c5d --- /dev/null +++ b/Sprint-1/2-mandatory-errors/4.js @@ -0,0 +1,2 @@ +const 12HourClockTime = "20:53"; +const 24hourClockTime = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js new file mode 100644 index 000000000..e24ecb8e1 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -0,0 +1,22 @@ +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +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 + +// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? + +// c) Identify all the lines that are variable reassignment statements + +// d) Identify all the lines that are variable declarations + +// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js new file mode 100644 index 000000000..47d239558 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -0,0 +1,25 @@ +const movieLength = 8784; // length of movie in seconds + +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +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? + +// b) How many function calls are there? + +// c) Using documentation, explain what the expression movieLength % 60 represents +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators + +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? + +// e) What do you think the variable result represents? Can you think of a better name for this variable? + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js new file mode 100644 index 000000000..60c9ace69 --- /dev/null +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -0,0 +1,27 @@ +const penceString = "399p"; + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); + +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + +console.log(`£${pounds}.${pence}`); + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// You need to do a step-by-step breakdown of each line in this program +// Try and describe the purpose / rationale behind each step + +// To begin, we can start with +// 1. const penceString = "399p": initialises a string variable with the value "399p" diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md new file mode 100644 index 000000000..e7dd5feaf --- /dev/null +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -0,0 +1,18 @@ +Open a new window in Chrome, + +then locate the **Console** tab. + +Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). +Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. + +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? + +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`? diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md new file mode 100644 index 000000000..0216dee56 --- /dev/null +++ b/Sprint-1/4-stretch-explore/objects.md @@ -0,0 +1,16 @@ +## Objects + +In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. + +Open the Chrome devtools Console, type in `console.log` and then hit enter + +What output do you get? + +Now enter just `console` in the Console, what output do you get back? + +Try also entering `typeof console` + +Answer the following questions: + +What does `console` store? +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? diff --git a/Sprint-1/readme.md b/Sprint-1/readme.md new file mode 100644 index 000000000..62d24c958 --- /dev/null +++ b/Sprint-1/readme.md @@ -0,0 +1,35 @@ +# 🧭 Guide to Week 1 exercises + +> https://programming.codeyourfuture.io/structuring-data/sprints/1/prep/ + +> [!TIP] +> You should always do the prep work _before_ attempting the coursework. +> The prep shows you _how_ to do the coursework. +> There is often a step by step video you can code along with too. +> Do the prep. + +This README will guide you through the different sections for this week. + +## 1 Exercises + +In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 2 Errors + +In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. + +## 3 Interpret + +In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. + +You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! + +You can also use `console.log` to check the value of different variables in the code. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 4 Explore - Stretch 💪 + +This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions. From 19f296f357140c9c65ac49715b2192e397432098 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Mon, 21 Jul 2025 19:44:56 +0100 Subject: [PATCH 23/26] convert kg to pounds and round to 2 decimal places --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 1d9d78770..4fe7537d8 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,11 +1,7 @@ // Define a function that converts kilograms to pounds function toPounds(kg) { const pounds = kg * 2.20462; - return pounds.toFixed(2); // rounds to 2 decimal places + return Number(pounds.toFixed(2)); // return as number, not string } -// Call the function with different inputs to test it -console.log(toPounds(1)); // 2.20 -console.log(toPounds(5)); // 11.02 -console.log(toPounds(70)); // 154.32 -console.log(toPounds(0)); // 0.00 +module.exports = toPounds; From f63026f3aded31286baee49a6de17a90417b0628 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Mon, 21 Jul 2025 19:50:06 +0100 Subject: [PATCH 24/26] Create reusable toPounds function from Sprint-1 code --- Sprint-2/3-mandatory-implement/3-to-pounds.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 4fe7537d8..d75d4291d 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -1,7 +1,22 @@ -// Define a function that converts kilograms to pounds -function toPounds(kg) { - const pounds = kg * 2.20462; - return Number(pounds.toFixed(2)); // return as number, not string +function toPounds(penceString) { + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + return `£${pounds}.${pence}`; } -module.exports = toPounds; +// Test calls +console.log(toPounds("399p")); // Output: £3.99 +console.log(toPounds("5p")); // Output: £0.05 +console.log(toPounds("45p")); // Output: £0.45 +console.log(toPounds("999p")); // Output: £9.99 +console.log(toPounds("2p")); // Output: £0.02 From a9c2a215bed092c7c7cff0bbc007da68a050ddad Mon Sep 17 00:00:00 2001 From: Samiuk Date: Mon, 21 Jul 2025 19:55:21 +0100 Subject: [PATCH 25/26] format 12-hour time consistently with 2-digit hours --- Sprint-2/5-stretch-extend/format-time.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 7c8bfeaa3..01c748172 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,26 +2,33 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); + let formattedHour; + let period; + if (hours === 0) { - // midnight - return `12:${minutes} am`; + formattedHour = "12"; + period = "am"; } else if (hours === 12) { - // noon - return `12:${minutes} pm`; + formattedHour = "12"; + period = "pm"; } else if (hours > 12) { - return `${hours - 12}:${minutes} pm`; + formattedHour = String(hours - 12).padStart(2, "0"); + period = "pm"; } else { - // 1am to 11am - return `${time} am`; + formattedHour = String(hours).padStart(2, "0"); + period = "am"; } + + return `${formattedHour}:${minutes} ${period}`; } + // Tests: const tests = [ { input: "00:00", expected: "12:00 am" }, { input: "08:00", expected: "08:00 am" }, { input: "12:00", expected: "12:00 pm" }, - { input: "15:30", expected: "3:30 pm" }, + { input: "15:30", expected: "03:30 pm" }, { input: "23:59", expected: "11:59 pm" }, { input: "11:15", expected: "11:15 am" }, ]; @@ -34,3 +41,4 @@ tests.forEach(({ input, expected }) => { ); }); + From 5eb1ab88e0830f8ad574c874efb1c3499002861b Mon Sep 17 00:00:00 2001 From: Samiuk Date: Tue, 22 Jul 2025 22:07:24 +0100 Subject: [PATCH 26/26] Refactor formatAs12HourClock to reduce code duplication Moved padStart(2, "0") to a single location after calculating rawHour. Improves readability and makes the function easier to maintain. Thanks for the helpful suggestion! --- Sprint-2/5-stretch-extend/format-time.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 01c748172..219dd8237 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -2,23 +2,25 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); - let formattedHour; + let rawHour; let period; if (hours === 0) { - formattedHour = "12"; + rawHour = 12; period = "am"; } else if (hours === 12) { - formattedHour = "12"; + rawHour = 12; period = "pm"; } else if (hours > 12) { - formattedHour = String(hours - 12).padStart(2, "0"); + rawHour = hours - 12; period = "pm"; } else { - formattedHour = String(hours).padStart(2, "0"); + rawHour = hours; period = "am"; } + const formattedHour = String(rawHour).padStart(2, "0"); + return `${formattedHour}:${minutes} ${period}`; }