Skip to content

Northwest | May-2025 | Sofayas Solomon | Sprint 2 #658

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
19 changes: 17 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,20 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// The error happened because the variable name str was used twice. First, it's used as a function parameter, and then it's declared again inside the function using let str, which is not allowed in JavaScript. You can't declare the same variable name twice in the same scope using let. That’s why we get a SyntaxError.

// i have two ways of fixing this code so i will write them one by one

// the first one is by using a different variable name

function capitalise(str) {
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalised;
}

// the second one is by reusing "str" without let

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
18 changes: 15 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here

// SyntaxError: Identifier 'decimalNumber' has already been declared
// ReferenceError: decimalNumber is not defined

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

Expand All @@ -14,7 +16,17 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
/* The error happens because we are trying to use console.log(decimalNumber) outside the function,
but decimalNumber is not defined in the global scope — it only exists inside the function.
Also, inside the function, we are using const decimalNumber = 0.5;, which tries to redeclare the function parameter.
That’s also not allowed — i can’t have both */
// this is almost the same with the first exercise i did(js 0.js)

// 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)); // Output: "50%"
19 changes: 15 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
//SyntaxError: Unexpected number


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

// =============> write the error message here
// syntaxError: Unexpected number


/* A function parameter cannot be a number — it must be a variable name.
In function square(3), the number 3 is not allowed in the function definition.
Instead, we should use a variable like num to receive values when the function is called. That’s why this gives a SyntaxError.*/


// =============> explain this error message here

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}

console.log(square(3)); // Output: 9



18 changes: 15 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
// Predict and explain first...

// =============> write your prediction here
// My prediction is that the code will print 320 because the function uses console.log to print the value of a * b, and I called the function with the arguments 10 and 32.
// am also predicting the code to print "The result of multiplying 10 and 32 is 320"

function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
/* the function `multiply(a, b)` uses `console.log()` to print the result of `a * b`.
However, the function does not return anything. That means its return value is `undefined`.
when we use `${multiply(10, 32)}` inside the template string, JavaScript calls the function — but it inserts `undefined` into the string because the function didn’t return a value.
So the output becomes:
"The result of multiplying 10 and 32 is undefined". */

// 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)}`);

// we have to use return instead of console.log()
17 changes: 14 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here

// the function will return `undefined` because the `return` statement is empty and ends immediately.
// So, the output will be: "The sum of 10 and 32 is undefined"

function sum(a, b) {
return;
Expand All @@ -8,6 +10,15 @@ function sum(a, b) {

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
/* In JavaScript, when the `return` statement is written with a semicolon immediately after it (`return;`),
the function returns `undefined` and stops executing.
The line `a + b;` is never calculated because it comes after the return.
so, the function doesn’t return the sum, and the string prints `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)}`);
36 changes: 32 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Predict and explain first...

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

// I expect all 3 lines to print:

// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

const num = 103;

Expand All @@ -14,11 +19,34 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
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

// my prediction and the out come is the same

// Explain why the output is the way it is
// =============> write your explanation here

/*Since the function doesn’t take any input (no parameter), it always uses the global variable num, which is set to 103.
So even if we try to pass a different number, the function still uses 103.
That means the output will always show the last digit of 103, which is "3".*/

// Finally, correct the code to fix the problem
// =============> write your new code here

// We need to change the function so it takes a parameter (a number),and uses that number instead of the hardcoded num.

function getLastDigit(number) {
return number.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

//The last digit of 42 is 2
//The last digit of 105 is 5
//The last digit of 806 is 6

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
10 changes: 8 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,11 @@
// 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 bmi.toFixed(1); // toFixed(1) means: show 1 number after the decimal point (e.g. 23.41 becomes 23.4).
}

console.log(calculateBMI(96, 1.79)); // output: 30.0

// toFixed(1) returns a string, not a number
// if i want to return a number i will use return number(bmi.tofixed(1))
7 changes: 7 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,10 @@
// 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 allCapsWithUnderScore(text) {
return text.toUpperCase().replaceAll(" ", "_");
}

console.log(allCapsWithUnderScore("hello there")); // Output: HELLO_THERE
console.log(allCapsWithUnderScore("lord of the rings")); // Output: LORD_OF_THE_RINGS
26 changes: 26 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,29 @@
// 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 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}`;
}

console.log(toPounds("399p")); // £3.99
console.log(toPounds("99p")); // £0.99
console.log(toPounds("1p")); // £0.01
console.log(toPounds("5p")); // £0.05
console.log(toPounds("0p")); // £0.00
23 changes: 18 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,37 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

formatTimeDisplay(61);

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// pad() is called 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: (1-1)/60

// c) What is the return value of pad is called for the first time?
// =============> write your answer here

// pad (0) = "00": as pad(num) is saying Make the string at least 2 characters long.

// If it’s too short, add "0" at the beginning.

// 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

// remainingSeconds = seconds % 60
// seconds = 61
// 61%60 we will take the remainder which is 1 so the assigned value to num is 1
// % is called Division Remainder

// 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

// pad(1)= "01"
// pad(1) will be changed to at least a two character string
51 changes: 51 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,54 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// MAIN PROBLEMS (BUGS)

// Fails because 12:00 is noon → should be "12:00 pm"
console.assert(
formatAs12HourClock("12:00") === "12:00 pm",
" Bug: Expected 12:00 to become 12:00 pm (noon)"
);

// Fails because 00:00 is midnight → should be "12:00 am"
console.assert(
formatAs12HourClock("00:00") === "12:00 am",
" Bug: Expected 00:00 to become 12:00 am (midnight)"
);

// Fails because 13:00 should become "01:00 pm", but function returns "1:00 pm"
console.assert(
formatAs12HourClock("13:00") === "01:00 pm",
" Bug: Expected 13:00 to become 01:00 pm (needs padded hour)"
);

// Fails because function always adds ":00", so 13:30 would return "1:00 pm"
// 00 is hardcoded
// needs padded hour as well
console.assert(
formatAs12HourClock("13:30") === "01:30 pm",
" Bug: Expected 13:30 to become 01:30 pm (minutes not preserved)"
);

// FIXED CODE

function formatAs12HourClock(time) {
const hours24 = Number(time.slice(0, 2));
const minutes = time.slice(3); // Keep the real minutes

let period = "am";
let hours12 = hours24;

if (hours24 === 0) {
hours12 = 12;
} else if (hours24 === 12) {
period = "pm";
} else if (hours24 > 12) {
hours12 = hours24 - 12;
period = "pm";
}

// Pad hour if less than 10
const hourString = hours12 < 10 ? `0${hours12}` : `${hours12}`;
return `${hourString}:${minutes} ${period}`;
}