Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ function capitalise(str) {
}

// =============> write your explanation here
// The error occurred because str is declared twice. Instead the expression is returned directly without declaring a new variable

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

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
const newName = capitalise("");
console.log(newName);
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// decimalNumber is declared twice and passed a value inside the function.
// The function hasn't been called properly

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

function convertToPercentage(decimalNumber) {
Expand All @@ -16,5 +19,17 @@ console.log(decimalNumber);

// =============> write your explanation here

// The error occurred because decimalNumber is declared twice. No need to create the percentage variable too.
// Instead the expression is returned directly without declaring a new variable.
// The function should be called with a value passed in as an argument and the result should be stored in a variable to be logged to the console.
// convertedPercentage is declared to store the result of the function call and then logged to the console.

@Liam310 Liam310 Jul 6, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a great explanation of the errors, just bear in mind that the sentence:

No need to create the percentage variable too

is a matter of opinion! No need to comment on the cleanliness of the code here 🙂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That is a good point. I will bear that in the mind when commenting on a code next time :)


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

function convertToPercentage(decimalNumber) {
return`${decimalNumber * 100}%`;
}

const convertedPercentage = convertToPercentage(0.5);
console.log(convertedPercentage);
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@

// =============> write your prediction of the error here

// num is not declared as a result undefined error will be displayed.

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

// =============> write the error message here

// /Users/Russom/Desktop/x.js:1
//function square(3) {
//SyntaxError: Unexpected number

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

// The error message indicates that 3 cannot be passed as a parameter in the function declaration.
// The parameter should be a variable name not a number.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3));

// The output of the function will be 9 as 3 * 3 = 9.
17 changes: 17 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

// =============> write your prediction here

// the function won't throw error. However, the function doesn't
// have a return expression. Therefore the function is called inside
// the console.log it will give undefined function.

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

// =============> write your explanation here

// 320
//The result of multiplying 10 and 32 is undefined

// The above error message is displayed when tested with node.
// The console.log inside the function outputs the correct value(320)
// However the function multiply does not return value so when the multiply function is called on line 9 it returns 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)}`);
14 changes: 14 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

// This will throw an error because there is a semicolon after return statement.

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

// =============> write your explanation here

//The sum of 10 and 32 is undefined

// The above error message is displayed when tested with node.
// The cause of the error message is the semicolon that is placed after the return statement.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did this actually cause an error message to be displayed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It returns '' The sum of 10 and 32 is undefined". Initially I had assumed that this was happening because of the semicolon after return. Now I run the code again this time without the semicolon still get "undefined" message. I did some research online and it is actually not just because of the semicolon but because both the return statement and the expression "a+b" are not on the same line.


// 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)}`);
23 changes: 23 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Predict the output of the following code:
// =============> Write your prediction here

// num is declared outside of the function. So when getLastDigit is called it will return undefined.

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +17,31 @@ 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

// 1) The above output (3) is displayed because the variable num is declared as a constant
// 2) .slice(-1) is used to get the last digit of num inside the function.
// 3) The function does not take any parameters so when it is called with an arguments it always returns the last digit of the constant num 103.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would this be different if the variable was declared with let?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No. I realise that the error is actually not because it declared as const. num isn't defined as the function's parameter so the function can take arguments later on when it's called.

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

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

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

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

// Now the num is passed to the function as a parameter.
// Therefore the function will return the last digit of the number when it is when it is called and passed with an argument.
8 changes: 7 additions & 1 deletion 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 (weight / (height * height)).toFixed(1);
// return the BMI of someone based off their weight and height
}
}

console.log(` Your Body Mass Index is: ${calculateBMI(70, 1.73)}`);

// On line 19 after weight and heigh are calculated to give the BMI.
// The .toFixed(1) method is used to round the BMI to 1 decimal place.
10 changes: 10 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,13 @@
// 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 upperSnakeCase(str) {
return str.toUpperCase().split(" ").join("_");
}

console.log(upperSnakeCase("upper snake case"));

// 1) On line 19 the return statement inside the function first converts the strings to uppercase using toUpperCase()
// 2) Then the split() method is used to split the strings into an array of words.
// 3) Finally the join() method is used to join the words inserting an underscore between each word.
44 changes: 44 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,47 @@
// 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(
// p is removed from the string with the sub string method.
0,
penceString.length - 1
// length - 1 is used to get the index of the last character in penceString, which is p.
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// padStart is used to ensure that the string has at least 3 characters by adding 0 at the start of the string
// if it is less than 3 characters long. This is important for formatting purposes,
// as we want to ensure that we have at least 3 digits to represent pounds and pence.
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
// substring is used to extract the pounds portion of the string by taking all characters except the last two,
// which represent pence.
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
// substring is used to extract the pence portion of the string by taking the last two characters.
// padEnd is used to ensure that the pence portion has at least 2 characters by adding 0 at the end of the string
// if it is less than 2 characters long. This is important for formatting purposes,
// as we want to ensure that we have at least 2 digits to represent pence.
return `£${pounds}.${pence}`;
// finally, the function returns a string representing the price in pounds,
// formatted with a £ symbol and a decimal point separating pounds and pence.
}
console.log(toPounds("399p"));
console.log(toPounds("1p"));
console.log(toPounds("3999p"));
// above are some test cases to check if the function works correctly for different inputs.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These comments are a hangover from the task in Sprint 1 (as are lots of the comments you have in the body of the function) - when copying code (and this goes for any time you do it!) make sure you are only bringing over the relevant stuff, and anything that isn't relevant should be excluded

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok. Noted.

10 changes: 10 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// pad will be called 3 times when formatTimeDisplay is called. Once for totalHours, once for remainingMinutes and one for remainingSeconds.

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

// The value assigned to num when pad is called for the first time is 0. This is because totalHours is calculated as (totalMinutes - remainingMinutes) / 60, which results in 0 when the input is 61 seconds.

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

// The return value of pad when it is called for the first time is 00. this is because the value of num is 0 and the pad function adds a 0 to the front of the string until the num characters has at least 2 characters.

Comment on lines +38 to +39

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To be precise, what exactly is the return value of pad? I think you know what it is, but 00 doesn't quite exactly describe it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi Liam. Thanks for reviewing my code.
The exact return value is 0 . It ads a single 0 instead of two 0s.

// 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 value assigned to num when pad is called for the last time is 1. This is because the last call to pad is for remainingSeconds which is calculated as seconds % 60, the result of which is 1.

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here

// The return value of pad when it is called for the last time is 01. This is because the pad function adds a 0 to the front of the string if the characters of the string are less than 2.
Comment on lines +47 to +48

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above comment!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Again it will be a single 0.

75 changes: 67 additions & 8 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,81 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5); // The minute variable is added to manipulate the minutes by storing the original minutes as we are converting only the hours not the minutes.

if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; // Here String object is added to convert hours into strings so the padStart can be used. This is because the padStart method only works with strings.
} else if (hours === 12) {
return `12:${minutes} pm`;
} else if (hours === 0) {
return `12:${minutes} am`;
} else {
return `${time} am`;
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const currentOutput1 = formatAs12HourClock("08:00");
const targetOutput1 = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
currentOutput1 === targetOutput1,
`current output 1: ${currentOutput1}, target output: ${targetOutput1}`
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output 2: ${currentOutput2}, target output: ${targetOutput2}`
);

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output 3: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output 4: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAs12HourClock("00:01");
const targetOutput5 = "12:01 am";
console.assert(
currentOutput5 === targetOutput5,
`current output 5: ${currentOutput5}, target output: ${targetOutput5}`
);

const currentOutput6 = formatAs12HourClock("12:01");
const targetOutput6 = "12:01 pm";
console.assert(
currentOutput6 === targetOutput6,
`current output 6: ${currentOutput6}, target output: ${targetOutput6}`
);

const currentOutput7 = formatAs12HourClock("23:59");
const targetOutput7 = "11:59 pm";
console.assert(
currentOutput7 === targetOutput7,
`current output 7: ${currentOutput7}, target output: ${targetOutput7}`
);

const currentOutput8 = formatAs12HourClock("13:00");
const targetOutput8 = "01:00 pm";
console.assert(
currentOutput8 === targetOutput8,
`current output 8: ${currentOutput8}, target output: ${targetOutput8}`
);

// Edge test cases that should be tested:

// 1) 08:00 -> 08:00 am
// 2) 23:00 -> 11:00 PM
// 3) 00:00 -> 12:00 AM
// 5) 12:00 -> 12:00 PM
// 6) 00:01 -> 12:01 AM
// 7) 12:01 -> 12:01 PM
// 8) 23:59 -> 11:59 PM
// 9) 13:00 -> 1:00 PM