Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0fc35ed
Interpret error message and explain cause in 1-key-errors/0.js
Zadri415 Nov 8, 2025
f4e38d3
Fix and explain redeclaration SyntaxError in convertToPercentage (1-k…
Zadri415 Nov 8, 2025
8a162e3
Fix and explain 'unexpected number' SyntaxError in square() (1-key-er…
Zadri415 Nov 8, 2025
2bf3487
Fix undefined return in sum() and explain cause (2-mandatory-debug/0.js
Zadri415 Nov 8, 2025
54ca144
add changes to 2-mandatory-debug/0.js
Zadri415 Nov 9, 2025
ff14721
add prediction and explanation
Zadri415 Nov 9, 2025
97dc683
predict the output
Zadri415 Nov 9, 2025
2f8d74e
add and explain calculateBMI() function to compute BMI to one decimal…
Zadri415 Nov 9, 2025
c52e087
add using String.toUpperCase() to convert greeting to uppercase
Zadri415 Nov 9, 2025
0de1e19
turn interpret/to-pounds.js into a reuseable block of code
Zadri415 Nov 9, 2025
a451eda
add formatTimeDisplay() answers explaining pad() calls and returning …
Zadri415 Nov 9, 2025
083a9d2
add and test formatAs12HourClock function with passing assertions
Zadri415 Nov 10, 2025
f4c8d85
Fix console log to use multiply function
Zadri415 Nov 13, 2025
2776d16
Fix typo in function name from forma to format
Zadri415 Nov 13, 2025
9b67723
Merge branch 'CodeYourFuture:main' into coursework/sprint-2
Zadri415 Feb 13, 2026
1e55d56
Update 1.js
Zadri415 Feb 19, 2026
c74f8a2
Update 0.js
Zadri415 Feb 20, 2026
5cbb8bc
Update 0.js
Zadri415 Feb 20, 2026
34bc832
Update 2.js
Zadri415 Feb 20, 2026
f5fbe4d
Update 1.js
Zadri415 Feb 20, 2026
7596b5a
Update 1.js
Zadri415 Feb 20, 2026
c9214f3
Update 3-to-pounds.js
Zadri415 Feb 20, 2026
ee36551
Update 3-to-pounds.js
Zadri415 Feb 20, 2026
c05000b
Update 3-to-pounds.js
Zadri415 Feb 21, 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
8 changes: 8 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// It will show a error

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -8,6 +9,13 @@ function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
capitalise("Hello");

// =============> write your explanation here
// SyntaxError: Identifier 'str' has already been declared - This error means that the variable 'str' was declared more than once in the same scope.
// JavaScrip doesn't allow that - once a variable name is declared with let or const, you can't declare it again.
// =============> write your new code here
function capitalise(str) {
return str[0].toUpperCase() + str.slice(1);
}

17 changes: 17 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict that this program will throw a error message - due to the variable decimalNumber being redeclared

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

Expand All @@ -15,6 +16,22 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber
// Inside the function: const decimalNumber = 0.5; - this redeclares the same variable name that's already used for the parameter.
// That causes a error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let)
// with the same name as a parameter inside the same function scope.
// JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused a error

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

// Function decimalNumber = 0.5
// It calculates 0.5 * 100 = 50
// It returns "50%"
// console logs 50%
11 changes: 8 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ function square(3) {
}

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

// SyntaxError: Unexpected number
// =============> explain this error message here

// The "Unexpected number"
// Finally, correct the code to fix the problem

// The "Unexpected number" error arises when a number is improperly positioned or used within JavaScript code
// =============> write your new code here
function square(num){
return num * num;
}

console.log(square);


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

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

// It will show a undefined error
function multiply(a, b) {
console.log(a * b);
}

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

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

// The function printed the answer but did't return it, so when it was used in the template string, it came out as '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 multipy 10 and 32 is ${multiply(10, 32)}`);

7 changes: 7 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I predict it will show a undefined error

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

// =============> write your explanation here
// The problem was the semicolon after return, which made the function stop before adding the numbers

Choose a reason for hiding this comment

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

Could you review the code you have written below as the solution and the previous code to understand why the previous function (before your solution) raised an error and update the explanation?

// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b){
return a + b; // Once I put a + b on the same line as return, it worked
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
17 changes: 16 additions & 1 deletion 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

// Since getLastDigit() does not declare any parameters, the arguments passed when calling it are going to be ignored
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +15,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 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() does not have any parameters, but it is being called with arguments:
// getLastDigit(42), getLastDigit(105), and getLastDigit(806). Inside the function, it always uses the global const num, which is set to 103.
// The means every call ignores the last digit of 103. Which the '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
9 changes: 8 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

console.log(`Your BMI is ${calculateBMI(68, 1.75)}`);
// How it works: function calculateBMI(weight, height)
// Parameters: weight - number, person's weight in kilograms height - number, person's height in meters
// Returns: the body mass index (BMI), rounded to one decimal place
3 changes: 3 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,6 @@
// 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
const greeting = "Hello there."

console.log(greeting.toUpperCase());
9 changes: 9 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,12 @@
// 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(penceString){
const penceStringWithoutTrailingP = penceString.substring(0, penceString.length -1);
const pounds = paddedPenceNumberString.slice(0, -2);
return `£${pounds}.${pence}`;
}

console.log(formatPenceToPounds("9p"));
console.log(formatPenceToPounds("39p"));
console.log(formatPenceToPounds("399"));
12 changes: 11 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// Three 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 first time pad() run, its used for hours part of time.
Because 61 seconds equals 0 hours, the value given to num is 0. */
// c) What is the return value of pad is called for the first time?
// =============> write your answer here
/* The first time pad() runs, it's formatting the hours part of time.
Since the hours value is 0, it turns that into the string '00' by adding a leading zero before returning it. */

// 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
/* When the function runs with 61 seconds, the last time pad() is called is for the second part of the time.
Since 61 seconds has 1 second left over after making a full minute, the value passed into pad() is 1.
Inside the function, num equals 1, and it gets turned into '01' so the final time shows as 00:01:01. */

// 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
/* When pad() is called for the last time, num is 1 because there's 1 second left over.
The function turns that number into the string '01' by adding a leading zero.
That '01' is the value that gets returned and used as the seconds part of the final time display (00:01:01). */
53 changes: 52 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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) {
/*function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
Expand All @@ -22,4 +22,55 @@ const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
); */

// The function ignores the minutes and did'not handle midnight (00:00) or noon (12:00) properly.
// Better version:
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 `${time} am`;
if (hours === 12) return `${time} pm`;

return `${hours - 12}:${minutes} pm`;
}
const currentOutput1 = formatAs12HourClock("08:00");
const targetOutput1 = "08:00 am";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);
// Test failed: due to a typo
const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`

);

const currentOutput3 = formatAs12HourClock("13:00");
const targetOutput3 = "1:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3} target output: {targetOutput3}`
);

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

console.log(formatAs12HourClock("08:00"));
console.log(formatAs12HourClock("23:00"));
console.log(formatAs12HourClock("15:00"));
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("12:00") === "12:00 pm");

// The test passed, so my function gave the expected results.
// All the console.assert() tests passed.
// formatAs12HourClock() function works correctly for the inputs I tested.