Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1df46e8
Answer questions, explain and refactor code.
Sep 13, 2026
f00caed
Predict, fix, and explain code
Sep 13, 2026
507f02a
Uncomment out function
Sep 13, 2026
6f4e46f
Predict, explain and fix code
Sep 13, 2026
9aadee0
Predict, explain and fix code
Sep 13, 2026
6018cd5
Update comment
Sep 13, 2026
444757f
Predict, explain, and fix code
Sep 13, 2026
8245202
Predict, explain, and fix code
Sep 13, 2026
4064cd8
Implement a BMI calculating function
Sep 13, 2026
2bdebf4
Remove console.log
Sep 13, 2026
849b970
Implement upper snake case converter
Sep 13, 2026
c3b0ad5
Refactor code by removing padEnd method.
Sep 13, 2026
0f95d9b
Refactor code into reusable function
Sep 13, 2026
7b9c2e2
Remove Sprint 2 to-pounds exercise
Sep 13, 2026
e4bb558
Answer and explain how code works
Sep 14, 2026
3eee2ad
Ignore 3-to-pounds.js
Sep 14, 2026
6bece94
Write test for 24:00 and 12:00am
Sep 14, 2026
5eba71d
Add if condition to compare time
Sep 16, 2026
8ecde42
Add more ege case tets.
Sep 16, 2026
8c33243
Merge branch 'CodeYourFuture:main' into coursework/sprint-3
russom-g Sep 16, 2026
7d6f51d
Remove wrongly pushed percentage change file
Sep 16, 2026
db95486
Update code.
Sep 16, 2026
70e2b1b
Remove wrongly pushed percentage change file
Sep 17, 2026
47cee31
Remove wrongly pushed to-pounds file
Sep 17, 2026
a7d328c
Remove wrongly pushed gitignore file
Sep 17, 2026
21c9992
Restore Sprint 2 files
Sep 17, 2026
172a529
Remove redundant undeclared percentage variable
Sep 20, 2026
b0d0f7c
Delete not needed print statement
Sep 20, 2026
f39cb79
Remove padEnd method
Sep 20, 2026
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
3 changes: 1 addition & 2 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const pounds = paddedPenceNumberString.substring(
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
.substring(paddedPenceNumberString.length - 2);

console.log(`£${pounds}.${pence}`);

Expand Down
12 changes: 12 additions & 0 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Predict and explain first...
// =============> write your prediction here

// Prediction: the method toUpperCase return the character at index 0 of str as uppercase.
// slice method will return the characters of str starting at index 1 excluding the first character.
// the template literal combines the converted upper case first character and the rest of the string.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

Expand All @@ -10,4 +14,12 @@ function capitalise(str) {
}

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

// Explanation: 1. str declares twice.Hence it gives syntax error.
// Refactor: 2. instead of returning str return can directly send back the template literal expression.

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

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
14 changes: 14 additions & 0 deletions Sprint-3/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

// Prediction:
// 1. decimalNumber is declared twice and it will give syntax error.

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

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

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

// 2. console.log will return undefined as decimalNumber local scope declaration inside the function.
// 3. It's percentage not decimalNumber needs printing.

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

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

console.log(convertToPercentage(0.5));

// The function convertToPercentage can be called passing with an argument of the input.
14 changes: 14 additions & 0 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@

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

// Prediction:
// 1. This will give reference error.
//


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

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

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

// The function expects a parameter name and it shouldn't start with a number.
// num should be the parameter and later 3 can be passed as an argument.

// Finally, correct the code to fix the problem

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

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


10 changes: 10 additions & 0 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

// 'The result of multiplying 10 and 32 is 10 * 32' is printed.

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

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

// the function prints a * b. When presented with multiply(10, 32) it prints 10 * 32.

// 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)}`);
15 changes: 14 additions & 1 deletion Sprint-3/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

// it will return syntax error

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

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

// There is a semicolon after return.

// Finally, correct the code to fix the problem.
// Later I discovered having a line between return and a + b gives another undefined error message
// =============> write your new code here

function sum(a, b) {
return a + b;
}

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

25 changes: 25 additions & 0 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Predict the output of the following code:
// =============> Write your prediction here

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

const num = 103;

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

// Because the function is not given a parameter and it is being passed an argument the same time which it ignores.
// The function uses the global variable num instead and print 3

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

// The reason was the num variable wasn't declared as a parameter inside the function.
// At the same time the function is receiving arguments which the function ignores.
5 changes: 3 additions & 2 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place


function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1);
}
4 changes: 4 additions & 0 deletions Sprint-3/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
// 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(str) {
return str.toUpperCase().replaceAll(" ", "_");
}
13 changes: 13 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
// 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 toPound(priceInPence) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two things here. The task asks for a function called toPounds and this one is toPound. And it says to call it a number of times to check it works for different inputs, where there is only the one call with "399p". What else would you try it with?

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.

I tested it with: 0, 00, 000, 01, 9, 58749 ...

const penceDigits = priceInPence
.substring(0, priceInPence.length - 1)
.padStart(3, "0");

const poundsPart = penceDigits.substring(0, penceDigits.length - 2);

const pencePart = penceDigits.substring(penceDigits.length - 2);

return `£${poundsPart}.${pencePart}`;
}
console.log(toPound("399p"));
12 changes: 12 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(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
Expand All @@ -23,16 +24,27 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// pad is called 3 times.

// 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 that assigned to num is 0

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

// The return value of pad is: "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 value assigned to num is 1. This is because 1 is the last character in the string 61

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The value 1 is right, but have another look at why. remainingSeconds comes from seconds % 60 on line 12, not from taking the last character out of the string "61". What is 61 % 60, and is the result a string or a number? Answer e) has the same reasoning in 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.

61 % 60 is 61 divided by 60 which is 1(totalMinutes) and the leftover 1 assigned to remainingSeconds


// 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 is "01". when pad is called for the last time it takes the last character of the string 61.
// After checking remainingSeconds length which is less than 2 it adds a "0" and returns "01"
79 changes: 70 additions & 9 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,83 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

if (hours === 0) {
return `12:${minutes} AM`;
}
if (hours === 12) {
return `12:${minutes} PM`;
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${hours - 12}:${minutes} PM`;
}
return `${time} am`;
return `${time} AM`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const currentOutput1 = formatAs12HourClock("00:00");
const targetOutput1 = "12: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";
const currentOutput2 = formatAs12HourClock("00:01");
const targetOutput2 = "12:01 AM";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output 2: ${currentOutput2}, target output: ${targetOutput2}`,
);

const currentOutput3 = formatAs12HourClock("08:00");
const targetOutput3 = "08: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("12:01");
const targetOutput5 = "12:01 PM";
console.assert(
currentOutput5 === targetOutput5,
`current output 5: ${currentOutput5}, target output: ${targetOutput5}`,
);

const currentOutput6 = formatAs12HourClock("13:00");
const targetOutput6 = "1:00 PM";
console.assert(
currentOutput6 === targetOutput6,
`current output 6: ${currentOutput6}, target output: ${targetOutput6}`,
);

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

const currentOutput8 = formatAs12HourClock("23:59");
const targetOutput8 = "11:59 PM";
console.assert(
currentOutput8 === targetOutput8,
`current output 8: ${currentOutput8}, target output: ${targetOutput8}`,
);

// Edge test cases tested for:

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