Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
19 changes: 13 additions & 6 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
// =============> write your prediction here : There will be an error.

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

Choose a reason for hiding this comment

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

This function has another syntax error after you made some changes. Could you please fix it?

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

// =============> write your explanation here : Because str has been used(declared) twice. As an input(parameter) to the function(capitalise) and with let. We should used a different variable.

// =============> write your explanation here
// =============> write your new code here
// =============> write your new code here :
/*
function capitalise(str) {
let cap = `${str[0].toUpperCase()}${str.slice(1)}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

This works well but something to consider for future is that variable names should be more descriptive, so it's easier for readers of your code to understand what this variable holds.

return cap;
}
console.log(capitalise("str"));
*/
22 changes: 19 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here : The variable decimalNumber has been declared.

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

// The old code :
/*
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +15,22 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);
*/

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

// =============> write your explanation here :
/*
-The variable decimalNumber has been declared within the function as a parameter then declared again using const.
- The variable decimalNumber should be defined first.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here

//The correct code :
const decimalNumber = 0.5;

function convertToPercentage(decimalNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This works nicely but as an optional exercise, could you think of a way to simplify the code within this function?

const percentage = `${decimalNumber * 100}%`;
return percentage;
}

console.log(convertToPercentage(decimalNumber));
16 changes: 9 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@

// 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 : There will be a syntax error because the parameter name cannot be a number.

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 : /*The error message shows that there is an unexpected number in the function parameter name. Function parameters must be valid identifiers (like variable names) and cannot be numeric literals.*/

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3));
9 changes: 7 additions & 2 deletions Sprint-2/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 : There is no return statement for the parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please explain your prediction better? What does the no return statement for the parameters mean and how do you think it affects the function behaviour?


function multiply(a, b) {
console.log(a * b);
}

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

// =============> write your explanation here
// =============> write your explanation here : /* The function multiply does not return any value, it only logs the result to the console. It returns undefined. To fix this, we need to add a return statement in the multiply function. */
Copy link
Contributor

Choose a reason for hiding this comment

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

excellent explanation!


// 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)}`);
9 changes: 7 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here : A syntax error for the semicolon after return.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,11 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> write your explanation here : The semicolon after return at end of the statement. So there will be no value returned from the function.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are moving in the right direction but the explanation is not entirely correct. Think about this -- would just removing semicolon fix the function?


// 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: 19 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// Predict the output of the following code
// =============> Write your prediction here : The code did not set any parameter/s for the function getLastDigit. So it will always take the value of num which is 103 as a default value.

const num = 103;

Expand All @@ -14,11 +14,26 @@ 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 : The output should be as the return line (103."103".3).
/* 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 : Since .toString() converts the number to a string. The slice(-1) method extracts the last character from that string..

// 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
// >>>> because the pre declared value of num with const. So the function always returns the last digit of 103 which is 3.
// >>>> getLastDigit should take a parameter to work properly.
7 changes: 5 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return their Body Mass Index to 1 decimal place

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

Choose a reason for hiding this comment

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

This works fine but something to keep in mind is that .toFixed() would return a string. It can be okay in this case as we don't have a strict requirement for the return type but something to be careful about while using this method.

// return the BMI of someone based off their weight and height
}
console.log(calculateBMI(75, 1.74));
6 changes: 6 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,9 @@
// 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 toUpperSnake(name) {
const upperCase = name.toUpperCase(); // Convert the string to uppercase
return upperCase.replaceAll(" ", "_"); // Replace all spaces with underscores
}
console.log(snakeItUp("alaa tagi is my name"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you calling the right function here?

20 changes: 20 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,23 @@
// 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(penceToPound) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Very cleanly written code with super descriptive variable names. Great job!

const penceStringWithoutTrailingP = penceToPound.substring(0, penceToPound.length - 1); // Remove the P at the end.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // Make sure there 3 digits at least.
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
); // get the pounds part( - the last two digits).
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0"); // get the pence part( they should be 2 digits).
return `£${pounds}.${pence}`; // The final format.
}
console.log(toPounds("3990p"));
console.log(toPounds("1500p"));
console.log(toPounds("150p"));
console.log(toPounds("100p"));
console.log(toPounds("70p"));
console.log(toPounds("50p"));
console.log(toPounds("0p"));
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> write your answer here : three times in Line 11 with return.

// 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
// =============> write your answer here : 0, because totalHours is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> write your answer here : "00", because num is converted to string and padded to 2 characters with "0".

// 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
// =============> write your answer here : 1, because remainingSeconds is 1 on python tutor.

// 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
// =============> write your answer here : "00", because num is converted to string and padded to 2 characters with "0". That's mean "1" becomes "01".
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is a typo in this answer, could you please double check if it's correct?

95 changes: 93 additions & 2 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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.

// The main function:
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
Expand All @@ -10,16 +11,106 @@ function formatAs12HourClock(time) {
return `${time} am`;
}

// Tests:
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
); // It's output is correct

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
); // It's output is correct

const currentOutput3 = formatAs12HourClock("12:00");
const targetOutput3 = "12:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
); // It's output is incorrect because it returns "12:00 am" instead of "12:00 pm"

const currentOutput4 = formatAs12HourClock("00:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
); // It's output is incorrect because it returns "00:00 am" instead of "12:00 am"

const currentOutput5 = formatAs12HourClock("15:30");
const targetOutput5 = "3:30 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
); // It's output is incorrect because it returns "15:00 pm" instead of "3:30 pm"

const currentOutput6 = formatAs12HourClock("11:45");
const targetOutput6 = "11:45 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
); // It's output is correct

//Fixing the function:
function formatAs12HourClockFixed(time) {
Copy link
Contributor

@jennethydyrova jennethydyrova Oct 13, 2025

Choose a reason for hiding this comment

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

The test cases you added are good but some edge cases to consider:

  1. Passing invalid time such as "25:30" or "15:78"
    2 Passing an empty string "" as parameter

Edit: I edited my comment to make it simpler for you

Copy link
Author

Choose a reason for hiding this comment

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

hi @jennethydyrova i have corrected all the things that you have mentioned earlier and push it to git .
would you please see if it correct?

Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @Alaa-Tagi, could you please double check if all tests are passing this file? It looks like tests are correct but they would fail.

const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);
let period = "am";

if (hours === 0) {
return `12:${minutes} am`;
}
if (hours === 12) {
period = "pm";
}
if (hours > 12) {
return `${hours - 12}:${minutes} pm`;
}
return `${hours}:${minutes} ${period}`;
}

// Re-testing :
const currentOutput1 = formatAs12HourClockFixed("08:00");
const targetOutput1 = "8:00 am";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
); // It's output is correct

const currentOutput2 = formatAs12HourClockFixed("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
); // It's output is correct

const currentOutput3 = formatAs12HourClockFixed("12:00");
const targetOutput3 = "12:00 pm";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
); // It's output is correct

const currentOutput4 = formatAs12HourClockFixed("00:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
); // It's output is correct

const currentOutput5 = formatAs12HourClockFixed("15:30");
const targetOutput5 = "3:30 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
); // It's output is correct

const currentOutput6 = formatAs12HourClockFixed("11:45");
const targetOutput6 = "11:45 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
); // It's output is correct