Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1189fcd
Add age calculation functionality and HTML structure for age input
HadiVahidi20 Sep 29, 2025
78b6a33
Refactor age calculator layout and improve form structure
HadiVahidi20 Sep 29, 2025
ea977e5
addWnetListener Example added
HadiVahidi20 Sep 29, 2025
22c0fee
Refactor age calculator layout and improve form structure
HadiVahidi20 Sep 30, 2025
c346f52
percentage exersice added
HadiVahidi20 Sep 30, 2025
ea0fca9
Enhance percentage exercise layout and add area calculation functiona…
HadiVahidi20 Sep 30, 2025
b619d91
Implement 12-hour clock formatting function and add test cases
HadiVahidi20 Oct 10, 2025
fd7a6c5
Fix variable redeclaration error in convertToPercentage function
HadiVahidi20 Oct 10, 2025
3dcaa8f
Fix syntax error in square function by using a variable as parameter
HadiVahidi20 Oct 10, 2025
5740fd5
Fix multiply function to return the product of two parameters
HadiVahidi20 Oct 10, 2025
3f7a1aa
Fix explanation comments in sum function and restore correct implemen…
HadiVahidi20 Oct 10, 2025
dda6132
Fix getLastDigit function to accept parameters and return the correct…
HadiVahidi20 Oct 10, 2025
b380c10
Implement BMI calculation function and add test cases
HadiVahidi20 Oct 10, 2025
df56f18
Implement convertToUpperSnakeCase function with test cases
HadiVahidi20 Oct 10, 2025
f77a875
Implement toPounds function with test cases for converting pence to p…
HadiVahidi20 Oct 10, 2025
e0d3cbf
the time-format.js is done
HadiVahidi20 Oct 13, 2025
8d0bb83
removed the unnecessary brackets
HadiVahidi20 Nov 7, 2025
521e641
Add handling and test for 12:00 (noon) case in formatAs12HourClock
HadiVahidi20 Nov 7, 2025
006643b
Merge branch 'CodeYourFuture:main' into coursework/sprint-2
HadiVahidi20 Nov 7, 2025
bfb00af
Remove Sprint 1 files from Sprint 2 pull request
HadiVahidi20 Nov 11, 2025
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
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here
// =============> this function get the argument and as string then first letter in capital and the rest of string in lower case;

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

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

// =============> write your explanation here
// the problem is in line 8, because we can not use the "str" twice, currently once we used as parameter and again as variable,
// =============> write your new code here

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

}
console.log(capitalise("hello world"));
33 changes: 25 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@

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

// the problem is in line 10, because we can not use the "decimalNumber" twice, currently once we used as parameter and again as variable,
// this will cause an error "SyntaxError: Identifier 'decimalNumber' has already been declared"
// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
// }

console.log(decimalNumber);
// console.log(decimalNumber);

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

// by line 9 the computer we remember the name of the function and its parameter but still it doesn't do any thing.
// by line 10 the computer will try to declare a new variable with the same name as the parameter of the function
// and it will cause an error because we can not use the same name twice in the same scope. even if it accept it no matter what argument
//we pass to the function it will always take the value of the variable that we declared inside the function.
//line 11 the computer will try to calculate the percentage but it will not reach this line because of the error in line 10.
// line 13 the computer will try to print the value of decimalNumber but it will not reach this line because of the error in line 10. but if reach
// it will always print 0.5 no matter what argument we pass to the function.
// in this case the error is "SyntaxError: Identifier 'decimalNumber' has already been declared" but if we fix it ,
//then then console will give us the decimal number that we pass to the function in percentage format.
// 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(.45));
20 changes: 17 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// in javaScript function parameters should be variable names, but in line 10 we used a number "3" as parameter,
// so the error will be "SyntaxError"

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

// =============> write the error message here
// function square(3) {
// ^

// SyntaxError: Unexpected number

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

// the problem is in line 10, because we can not use a number as parameter of the function, it should be variable name,
// so the error is "SyntaxError: Unexpected number"

// Finally, correct the code to fix the problem

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

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


console.log(square(3));
25 changes: 21 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
// Predict and explain first...

// =============> write your prediction here
// in this code we have a function called multiply that takes two parameters a and b,
//inside the function it will print the result of multiplying a and b,
// but a and b are not defined, not in global scope nor local scope, so we will get error.
// there is another console log statement that will print a string with the result of multiplying 10 and 32,
// but the function multiply does not return any value, so the result will be undefined.
// so the final output will be "The result of multiplying 10 and 32 is undefined"

function multiply(a, b) {
console.log(a * b);
}
// 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 multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// it wasn't completely as I predicted, the first console log statement inside the function multiply did worked and
// it printed 320, which means that the function parameters a and b took the values 10 and 32 from the function call multiply(10, 32).
// but the second console log statement printed "The result of multiplying 10 and 32 is undefined" because the function multiply does not return any value,
// so the result of multiplying 10 and 32 is undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
// I will add a return statement to the function multiply that returns the result of multiplying a and b.

function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
24 changes: 19 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// in this code we have a function called sum that takes two parameters a and b,
// inside the function there is a return statement followed by a; and then an expression a + b;
// but in JavaScript, when a return statement is executed, the function exits immediately and any code after the return statement is not executed.
// so the function will always return undefined because there is no value after the return statement.
// so the final output will be "The sum of 10 and 32 is undefined"

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

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

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

// it behaved exactly as I predicted, the console log statement printed "The sum of 10 and 32 is undefined"
// Finally, correct the code to fix the problem
// =============> write your new code here
// to correct the code we just need to remove the semicolon after the return statement.

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
32 changes: 24 additions & 8 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

// Predict the output of the following code:
// =============> Write your prediction here
//the function has no parameter so I am not sure if it get the 42, 105 and 806 as input or it will always use the value of num which is 103,
// but I know it change the number to string and get the last digit using slice method, because of the -1 index that means the last character.
// const num = 103;

const num = 103;
// function getLastDigit() {
// return num.toString().slice(-1);
// }

function getLastDigit() {
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)}`);
// 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)}`);

// 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
// It happens because, as I predicted, the function always considers 103 as its argument

// 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)}`);
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
5 changes: 4 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,7 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1);
}
console.log(calculateBMI(70, 1.73));
console.assert(calculateBMI(80, 1.73) === '26.7');
9 changes: 9 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,12 @@
// 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 convertToUpperSnakeCase(str) {
let upperCase= str.toUpperCase();
return upperCase.split(" ").join("_");

}
console.log(convertToUpperSnakeCase("hello there"));

console.assert(convertToUpperSnakeCase("hello there") === "HELLO_THERE", "Test Case 1 Failed");
console.assert(convertToUpperSnakeCase("lord of the rings") === "LORD_OF_THE_RINGS", "Test Case 2 Failed");
13 changes: 13 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,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 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"));
console.log(toPounds("9p"));
console.log(toPounds("85p"));
console.log(toPounds("1200p"));
// it works for all the test cases
10 changes: 9 additions & 1 deletion Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ 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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// it will be called 3 times, because in the return statement we called it 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
// the value assigned to num is totalHours which is 0.

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

// it 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 when pad is called for the last time is 1, because remainingSeconds = 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 return value of pad(1) is "01".
//That’s because inside pad, the number 1 becomes the string "1", and then padStart(2, "0") adds a leading zero to make it two digits.
41 changes: 41 additions & 0 deletions Sprint-2/Prep/12hours.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function formatAs12HourClock(time) {
const hour = Number(time.slice(0, 2));
const minute = time.slice(3);

if (hour === 0) {
return `12:${minute} am`;
} else if (hour === 12) {
return `12:${minute} pm`;
} else if (hour > 12) {
return `${hour - 12}:${minute} pm`;
} else {
return `${time} am`;
}
}
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

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

const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);
Empty file added Sprint-2/Prep/elevator.js
Empty file.