Skip to content

London | May-2025 | Hendrine Zeraua | STD Sprint-2 #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...
// =============> write your prediction here

// - The code will throw a SyntaxError due to variable redeclaration.
// call the function capitalise with a string input
// - The function provided contains a syntax error because it attempts to redeclare
// - the parameter str within the function using let, which is not permitted
// - in JavaScript. Below is the corrected version of the function, followed by an example of how it can be called.
// interpret the error message and figure out why an error is occurring

function capitalise(str) {
Expand All @@ -10,4 +13,22 @@ function capitalise(str) {
}

// =============> write your explanation here
// - When code run, this "SyntaxError: Identifier 'str' has
// - already been declared" will show
// - In the function capitalise(str), the parameter str is already declared.
// - But inside the function, you're trying to declare a new variable
// - with the same name using let str = ....
// - JavaScript doesn't allow declaring a new variable with the
// same name as a function parameter using let or const within the same scope.
// =============> write your new code here
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
Comment on lines +24 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

The function block is not properly closed.

Copy link
Author

Choose a reason for hiding this comment

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

Hi CJ,
I've added the missing curly braces for the function capitalise but for some reason I cannot spot the modified files in Sprint-1. Could you please direct me? I did check Sprint-1 - key-exercises - 3- paths.js and 3-mandatory-interpret - 1-percentage-change.js without success.
I appreciate your help reviewing this.

Copy link
Author

Choose a reason for hiding this comment

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

Hi CJ,

I've now reverted the changes made to the Sprint-1 files as requested. Let me know if there's anything else you'd like me to update. Thanks for reviewing!

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much. 🙏🏿


}

capitalise(hello);
console.log(capitalise('hello'));
// I used "hello" as an example input when
// calling the capitalise function:
// Output: 'Hello'
42 changes: 42 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// Predict and explain first...





// Why will an error occur when this program runs?
//
// -
// =============> write your prediction here
// - Goal: This function is meant to take a decimal (like 0.5)
// - and return it as a percentage (like "50%").
// expected use:
convertToPercentage(0.5);
// - should return "50"
// - What we expect:
// - Input: 0.5
// - Multiply by 100: 0.5 * 100 = 50
// - Convert to string with %: "50%"
// - Return "50%"



// Try playing computer with the example to work out what is going on

Expand All @@ -14,7 +32,31 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

Play computer:
function convertToPercentage(decimalNumber){
const decimalNumber = 0.5; // Error! 'decimalNumber' already declared
const percentage = `${decimalNumber * 100}%`;
return percentage;

}
// - The moment the engine sees const decimalNumber = 0.5;, it throws an error
// - because decimalNumber was already defined as a parameter.
// - So, the function never runs past that point.

// =============> write your explanation here
// - - SyntaxError: Identifier 'decimalNumber' has already been declared
// - This is because of this...
// - The variable decimalNumber is declared twice:
// - First, as a parameter in the function declaration:
// - function convertToPercentage(decimalNumber)
// - Then again, inside the function body:
// - const decimalNumber = 0.5;
// - JavaScript does not allow a const to be re-declared using
// - the same name as a parameter within the same function scope. This results in a syntax error.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
31 changes: 31 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,48 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// - This function is intended to square any number passed to it — meaning,
// - if you pass in 3, it should return 9.
// - However, the code as written will produce a syntax and reference error for two reasons:
// - 3 is not a valid parameter name — parameter names must be identifiers (like num, x, etc.).
// - The variable num is used but never declared inside the function.
// - Prediction of the Error
// - We expect two issues:
// - JavaScript will throw a syntax error because 3
// is not a valid parameter name.
// - If the syntax were somehow ignored, the code would
// later throw a ReferenceError because num is undefined.


function square(3) {
return num * num;
}

// =============> write the error message here
// - The first (and stopping) error would be:
// - SyntaxError: Unexpected number
// - If one fixed the syntax but left num undeclared, one'd see:
// - ReferenceError: num is not defined

// =============> explain this error message here
// - SyntaxError: Unexpected number
// - This means JavaScript found a number (3) where it
// - expected a variable name — numbers can't be used as parameter names.
// - ReferenceError: num is not defined
// - This happens when the code tries to use a
// - variable num that hasn’t been declared or passed in.


// Finally, correct the code to fix the problem

// =============> write your new code here

// - We need to use a valid parameter name like num
// - Make sure we use that parameter consistently in the return statement.
function square(num) {
return num * num;
}
// Example usage:
console.log(square(3)); // Output: 9
console.log(square(5)); // Output: 25

20 changes: 20 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Predict and explain first...

// =============> write your prediction here
// This answer is similar to the description below.
// - Would expect to print:
// - The result of multiplying 10 and 32 is 320
// - But based on how the function is written, here's what will actually happen:
// - The result of multiplying 10 and 32 is undefined


function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +15,20 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// - The code is trying to multiply the values 10 and 32 and display the result inside a sentence:
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// - However, the function multiply(a, b) only logs the result using
// - console.log(a * b) — it doesn't return the value.

// - Since there’s no return statement, the function returns undefined by default.
// - As a result, the template string ends up displaying:
// - The result of multiplying 10 and 32 is undefined
// - Even though the actual product (320) is printed on a separate line,
// - it’s not included in the sentence.
// 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)}`); // Output: 320
18 changes: 18 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...
// =============> write your prediction here
// - Expecting the code to print 'the sum of 10 and 32 is 42'
// - Because it's calling a function sum(10, 32) and trying to insert the result into a sentence.


function sum(a, b) {
return;
Expand All @@ -9,5 +12,20 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// - The function includes this:
return;
a + b;
// - In JavaScript, when one write return; on its own line,
// - the function immediately exits and returns undefined.
// - The line a + b; is never run, because the function already returned.
// - This is due to JavaScript's automatic semicolon insertion — it treats return; as a complete statement.
// - The actual Output, the sum of 10 and 32 is undefined


// 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)}`);
42 changes: 40 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Predict the output of the following code:
// =============> Write your prediction here

// - I think this will just keep returning the last digit of 103, no matter what number I pass in.
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +15,48 @@ 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 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
// Finally, correct the code to fix the problem
// 1. How the function is defined matters:
// The function getLastDigit() is defined without any
// parameters—its parentheses are empty:
function getLastDigit() { ... }
// 2. No parameters means no inputs:
// Even though one call the function with values like
// getLastDigit(42), getLastDigit(105), or getLastDigit(806),
// these values are ignored because the function
// definition does not accept any arguments.
// 3. Uses the global variable num:
// Inside the function, it only looks at the global variable:
const num = 103;
// So, no matter what one passes in, the function always uses 103.
// 4. Returns last digit of num:
// The function converts num (103) to a string
// and takes the last character:
num.toString().slice(-1) // returns "3"
// 5. Therefore, the output is always "3":
// When you run:
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// It prints: The last digit of 42 is 3
// Because the function ignores the 42 and returns the last digit of 103.
// Finally, correct the code to fix the problem
// =============> write your new code here

const num = 103;

function getLastDigit(num) {
return num.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'

// To make it work the function must be defined with a parameter (like num) inside the
// parentheses so it can accept different numbers when called. This way, the
// function works with any input number instead of always using a fixed global value.

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
11 changes: 9 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1));
}
console.log(calculateBMI(70, 1.73)); // Output: 23.4

// Step-by-step Calculation:
// Height squared = 1.73 × 1.73 = 2.9929
// BMI = 70 / 2.9929 ≈ 23.38
// Rounded to 1 decimal place = 23.4
21 changes: 21 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,24 @@
// 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

function toUpperSnakeCase(input) {
const upperSnake = input.split(' ').join('_').toUpperCase();
return upperSnake;
}

console.log(toUpperSnakeCase("I always enjoy the class at CYF on Saturdays"));
// Expected output: "I_ALWAYS_ENJOY_THE_CLASS_AT_CYF_ON_SATURDAYS

// This is what I did.
// I wrote a function called toUpperSnakeCase that takes a sentence and turns it into UPPER_SNAKE_CASE.
// Inside the function, I:
// Split the string into words using .split(' ')
// Joined the words with underscores using .join('_')
// Changed all letters to uppercase using .toUpperCase()
// Stored the result in a const variable called upperSnake
// Returned that final result
// Then I tested the function with the sentence:
// "I enjoy the class at CYF on Saturdays",
// and it returned: "I_ENJOY_THE_CLASS_AT_CYF_ON_SATURDAYS"

32 changes: 32 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,35 @@
// 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

function formatPenceToPounds(amountInPence) {
// Remove the "p" at the end of the string (e.g., "399p" → "399")
const penceStringWithoutTrailingP = amountInPence.substring(
0,
amountInPence.length - 1
);

// Add leading zeros so it’s at least 3 digits (e.g., "9" → "009")
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Get all but the last 2 digits as the pounds part (e.g., "399" → "3")
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// Get the last 2 digits as the pence part (e.g., "399" → "99")
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// Return the formatted result in pounds (e.g., "£3.99")
return `£${pounds}.${pence}`;
}

// Different inputs:
console.log(toPounds("499p")); // £4.99
console.log(toPounds("99p")); // £0.99
console.log(toPounds("7p")); // £0.07
console.log(toPounds("0p")); // £0.00
console.log(toPounds("1234p"));// £12.34
14 changes: 14 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times, once for hours, minutes and seconds.

// Call formatTimeDisplay with an input of 61, now answer the following:
// This happens when formatTimeDisplay(61) is called:
// remainingSeconds = 61 % 60 = 1
// totalMinutes = (61 - 1) / 60 = 60 / 60 = 1
// remainingMinutes = 1 % 60 = 1
// totalHours = (1 - 1) / 60 = 0 / 60 = 0

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// The answer is 0, because first call is pad(totalHours), and totalHours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The answer is "00", because pad(0) → 0.toString() → "0" → "0".padStart(2, "0") → "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
// The answer is 1. The last call is pad(remainingSeconds). From earlier: remainingSeconds = 1, so num = 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
// The answer is "01". Pad(1) → 1.toString() → "1" → "1".padStart(2, "0") → "01"


Loading