Skip to content

LONDON | May-2025 | Aida Eslamimoghadam| Module-Structuring-and-Testing-Data | Sprint-2 #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 16 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
// =============> ANSWER
// It takes a string and change its first letter to capitial letter aida -----> Aida

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

// // =============> ANSWER
// SyntaxError: Identifier 'str' has already been declared
// It is not allow to redeclare the parameter str inside the function

// // =============> write your new code here
let name = "aida";

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("aida"));
32 changes: 24 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
// Predict and explain first...
// =============> ANSWER
// convertToPercentage function takes a number and multiplaited it to 100,to give the percentage and then return it.
// but it doesn't work because we did'nt call it in the main part.
// Also we can not declare a variable which has been declared already

// Why will an error occur when this program runs?
// =============> write your prediction here
// we can not declare a variable which has been declared already
// 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
// =============> ANSWER
// It doesn't work because we did'nt call the function.

// Finally, correct the code to fix the problem

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

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

return percentage;
}

console.log(convertToPercentage(decimalNumber));
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@

// Predict and explain first BEFORE you run any code...
// This function does'nt work because we can not set literal value as a parameter.

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

// =============> write your prediction of the error here
// An error for number instead of parameter

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

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

// SyntaxError: Unexpected number

// =============> explain this error message here
// JavaScript is expecting a variable name (parameter) not a literal value

// Finally, correct the code to fix the problem

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

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

console.log(square(5));
17 changes: 13 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Predict and explain first...
// function should multiply a by b and print the answer. and it DOESN'T return .
// So in the Console.log we don't have the result.

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

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
// we don't have result for function because there is no return in function . "The result of multiplying 10 and 32 is 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)}`);
21 changes: 15 additions & 6 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// The code doesn't work because the expression a + b comes after the return statement
// Once the compiler reaches return it exit the function.
// There is no error. we just don't have answer
// 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
// There is no error. we just do not have correct answer.===> The sum of 10 and 32 is undefined
// 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)}`);
35 changes: 27 additions & 8 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// This code should return the last digit of each number but again it doesn't return a correct number because it ask to slice(-1) of "num"
// Predict the output of the following code:
// 3
// 3
// 3

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
// 3 3 3
// Explain why the output is the way it is
//

// =============> write your explanation here
// because we didn't send our parameter to function.

// 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
10 changes: 8 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,11 @@
// 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
}
let squaredHeight = height * height;
let BMI = weight / squaredHeight;
BMI = Number(BMI.toFixed(1));
return BMI;
}
let result = calculateBMI(70, 1.73);

console.log(`The BMI for a person weighing 70kg and 1.73m tall is ${result}`);
8 changes: 8 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,11 @@
// 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 UPPER_SNAKE_CASE(NewWord) {
let UpperWord = NewWord.toUpperCase();
let result = UpperWord.replaceAll(" ", "_");
return result;
}

console.log(UPPER_SNAKE_CASE("hello aida hope you are well"));
33 changes: 33 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,36 @@
// 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

const penceString = 0;
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 };
}

for (let i = 0; i < 5; i++) {
let randomNumber = Math.floor(Math.random() * 100001);

PenceRandom = randomNumber + "p";
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not declare the variable and start its name with a lowercase letter?

Copy link
Author

Choose a reason for hiding this comment

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

Hi Cj,
Thank you for your review.
I am trying to fix this issu. I learnt CamelCase incorrectly and I know it use for build-in function.
sorry about it.
I also fixed Branch issu.
Many thanks
Aida


let result = toPounds(PenceRandom);

console.log(
`Random: ${randomNumber}p → Pounds: £${result.pounds}.${result.pence}`
);
}
16 changes: 11 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@ 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
// =============> 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
// =============> It 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
// =============> It is 1
// The last time pad() is called, it's for the seconds value. Since remainingSeconds = 1, num is assigned 1.
// pad(1) returns "01" to format the time as hh:mm:ss

// 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
// =============> 01
//The last call to pad() formats the seconds.
// remainingSeconds is 1, so pad(1) is called.Inside pad, 1 becomes "1" (string), and since it's a single digit, it's padded to "01".
// So the return value is "01".
55 changes: 51 additions & 4 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,70 @@
// 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) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const [hourStr, minuteStr] = time.split(":");
let hours = Number(hourStr);
let suffix = "am";

if (hours === 0) {
hours = 12;
} else if (hours === 12) {
suffix = "pm";
} else if (hours > 12) {
hours -= 12;
suffix = "pm";
}
return `${time} am`;

const formattedHour = String(hours).padStart(2, "0");
return `${formattedHour}:${minuteStr} ${suffix}`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.log(
`The current time is 08:00 in 24-hour style, and this is ${targetOutput} in 12-hour style.`
);

console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.log(
`The current time is 23:00 in 24-hour style, and this is ${targetOutput2} in 12-hour style.`
);
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.log(
`The current time is 00:00 in 24-hour style, and this is ${targetOutput3} in 12-hour style.`
);
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("13:40");
const targetOutput4 = "01:40 pm";
console.log(
`The current time is 13:40 in 24-hour style, and this is ${targetOutput4} in 12-hour style.`
);
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAs12HourClock("22:15");
const targetOutput5 = "10:15 pm";
console.log(
`The current time is 22:15 in 24-hour style, and this is ${targetOutput5} in 12-hour style.`
);
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);