Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
00fdc77
solving exercise 0.js
AbdennourHachemi Sep 23, 2026
567077e
solving exercise 2.js
AbdennourHachemi Sep 23, 2026
c91ef99
solving 0.js
AbdennourHachemi Sep 23, 2026
0afe2a9
solving exercise 2-mandatory-debug 1.js
AbdennourHachemi Sep 24, 2026
292e899
solving exercise 2-mandatory-debug 2.js
AbdennourHachemi Sep 24, 2026
c7c8f3a
solving exercise 3-mandatory-implement 1-bmi.js
AbdennourHachemi Sep 24, 2026
a239738
solving the 2 3-mandatory-implements exercises
AbdennourHachemi Sep 24, 2026
63cc7a9
solve exercise 3-t-pounds.js
AbdennourHachemi Sep 24, 2026
c1fab78
Add time format functionality
AbdennourHachemi Sep 24, 2026
ca30eb5
solving format-time
AbdennourHachemi Sep 25, 2026
142dcbd
solving exercise 0.1
AbdennourHachemi Sep 29, 2026
5a1f129
uncommingting correct code to be able to run
AbdennourHachemi Sep 29, 2026
f95f159
fixing exercise 2.js
AbdennourHachemi Sep 29, 2026
f598953
fixing exercise 0.1 2-mandatory-debug
AbdennourHachemi Sep 29, 2026
f4b5d6a
fixing exercise 1-bmi.js
AbdennourHachemi Sep 29, 2026
cf05ff4
removing unnecessary console.log in line 6
AbdennourHachemi Sep 29, 2026
7947510
uncomment solution for exercies 1.js
AbdennourHachemi Sep 29, 2026
b503934
uncomment solution for exercies 2.js
AbdennourHachemi Sep 29, 2026
92e3132
correct num value
AbdennourHachemi Sep 29, 2026
514bece
fix formatting to exercise 1.js
AbdennourHachemi Sep 29, 2026
760800c
removing unused variable
AbdennourHachemi Oct 1, 2026
a1cc998
correct the answer for vlue assigned to num
AbdennourHachemi Oct 1, 2026
5698bf5
fix 1.js formmatting issue
AbdennourHachemi Oct 1, 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
14 changes: 10 additions & 4 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// Tha parameter 'str' is declared another time insie the function , this will cause an error to be thrown.

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

// function capitalise(str) {

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.

Your new code on lines 15 to 19 is in comments. So it never runs. The original code on lines 8 to 11 still runs. Run this file with node. What do you see? The broken code should stop running, and your fix should run. Can you swap them?

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.

Fixed now. Your fix runs, and the original is in comments. Good.

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

// =============> write your explanation here SyntaxError: Identifier 'str' has already been declared => line 10 varibale str should not be declared again.
// =============> write your new code
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("hello world"));
24 changes: 13 additions & 11 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here D: Answer => variable decimalNumber is declared twice : one time in the parameter and another time inside the function

// Try playing computer with the example to work out what is going on
// function convertToPercentage(decimalNumber) {
// //const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;
// return percentage;
// }

// console.log(decimalNumber);
// =============> write your explanation here
// solution :Also in line 15 console.log is trying print a variable which is not defined instead of calling the function convertToPercentage .
// Finally, correct the code to fix the problem
// =============> write your new code here

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

return percentage;
}

console.log(decimalNumber);

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

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(0.5));
19 changes: 10 additions & 9 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

// Predict and explain first BEFORE you run any code...

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

// =============> write your prediction of the error here
// =============> write your prediction of the error here : The funcation parameter 3 this will cause an error to be thrown because the parameter is not a valid identifier.
// function square(3) {
// return num * num;

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

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

// =============> explain this error message here
// =============> explain this error message here: SyntaxError: Unexpected number , the parameter 3 is not a valid identifier it should ba valid identifier like num.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3));
17 changes: 11 additions & 6 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// =============> write your prediction here : The function multipy does not return any value so the result of calling the function will be unknown.

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
// =============> write your explanation here : Calling the function has logged the result in the console but it has not returned the result ot the multiplicaion so the second console.log outside the function has no value to print and has printed 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)}`);
19 changes: 12 additions & 7 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: this code will give an error because the funcion does not return any value and calling it within the consle.log will print 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
// =============> write your explanation here : the sum a+b in line 6 should precede the return statement inorder to return the value.
// 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)}`);
33 changes: 22 additions & 11 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Write your prediction here: This code will throw an error because the function getLastDigit does not take any parameters but we are passing a parameter to it in the console.log statements.

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
// =============> write the output here :output
/* 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
// =============> write your explanation here: The funtion return one single value (3) no matter what parameter is passed to it because the function does not take any parameters and it is using the gobal variable num which is set to 103. So the last digit of 103 is always 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
// Explain why getLastDigit is not working properly - correct the problem : The first declaration of num as constent should be removed and the function getLastDigit should take a parameter num to return the last digit of the number passed to it.
3 changes: 3 additions & 0 deletions Sprint-3/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)); // should return 23.4
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(" ", "_");
}
console.log(toUpperSnakeCase("lord of the rings"));
28 changes: 28 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,31 @@
// 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("4798P"));
console.log(toPounds("31P"));
console.log(toPounds("0P"));
console.log(toPounds("-5576P"));
console.log(toPounds("4P"));
console.log(toPounds("832P"));
console.log(toPounds("200000P"));
17 changes: 10 additions & 7 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function pad(num) {
console.log(num)
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
Expand All @@ -15,24 +16,26 @@ 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
// =============> write your answer here: ***Answer*** :Function pad will be 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
// b) What is the value assigned to num when pad is called for the first time?:
// =============> write your answer here: ***Answer*** : The value assigned to num = 0.

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> write your answer here: ***Answer*** :The value of pad when it is called for the first time 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
// 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: ***Answer***: The last call of pad in this program is pad(remainingSeconds) => remainingSeconds= (61%60=1) => num = 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
// =============> write your answer here : ***Answer*** : The value of pad when is called for the last time is the third part in `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}` => pad(remainingSeconds)=pad(1) since 1 has a length <2 return will be padded with 0 => pad (1)return "01".
83 changes: 76 additions & 7 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,91 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = Number(time.slice(3, 5));

if (hours < 12) {
return `${time} am`;
} else if (hours == 12) {
return `${time} am`;
} else if (hours == 24) {
return `12:00 am`;
}

if (hours > 12 && hours < 22 && minutes < 10) {
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
}
if (hours > 12 && hours < 22 && minutes >=10) {
return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`;
}
if (hours >= 22 && minutes < 10) {
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
}
if (hours >= 22 && minutes >= 10) {
return `${hours - 12}:${minutes} pm`;
}
return `${time} am`;
}

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

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

const currentOutput4 = formatAs12HourClock("23:59"); // ***Asserrion failed : This needs fixing if hours > 12 minutes are not tracked and replaced by 00. =>fixec
// );
const targetOutput4 = "11:59 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
);

const currentOutput5 = formatAs12HourClock("12:00");
const targetOutput5 = "12:00 am";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`, // ***Asserrion failed : This needs fixing => fixed
);

const currentOutput6 = formatAs12HourClock("24:00"); // ***Asserrion failed : This needs fixing =>fixed
const targetOutput6 = "12:00 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
);

const currentOutput7 = formatAs12HourClock("12:01"); // ***Asserrion failed : This needs fixing =>fixed
const targetOutput7 = "12:01 am";
console.assert(
currentOutput7 === targetOutput7,
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
);

const currentOutput8 = formatAs12HourClock("22:10"); // ***Asserrion failed : This needs fixing =>fixed
const targetOutput8 = "01:10 am";
console.assert(
currentOutput8=== targetOutput8,
`current output: ${currentOutput8}, target output: ${targetOutput8}`,
);

const currentOutput9 = formatAs12HourClock("22:10"); // ***Asserrion failed : This needs fixing =>fixed
const targetOutput9 = "10:10 am";
console.assert(
currentOutput9 === targetOutput9,
`current output: ${currentOutput9}, target output: ${targetOutput9}`,
);

Loading