Skip to content

LONDON | May-2025 | Anuar Duisenbek | Module-Structuring-and-Testing-Data | Sprint-2 #559

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 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.idea/
.DS_Store
.vscode
**/.DS_Store
16 changes: 16 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
// Predict and explain first...
// =============> write your prediction here
//
// I predict we will make mistake because str has been already declared in body of function

Copy link

@rohan-corbyn rohan-corbyn Jul 6, 2025

Choose a reason for hiding this comment

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

Yes: The variable "str" is declared in the function signature as a parameter, it is then illegally redeclared in the body of the same function.

// call the function capitalise with a string input

//Uncaught SyntaxError: Identifier 'str' has already been declared

// interpret the error message and figure out why an error is occurring

//'str' has already been declared.Also we call function,defind it,but we don't use it.

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

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

//So I change : I just return str directly ,add a function call and console.log():

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

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

console.log(capitalise("Anuar"));
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

/*SyntaxError: Identifier 'decimalNumber' has already been declared*/
//Because same name is used again with const decimalNumber = 0.5;
// inside the function.

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

function convertToPercentage(decimalNumber) {
Expand All @@ -16,5 +20,16 @@ console.log(decimalNumber);

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

// console.log(decimalNumber); line is outside the function,
// so the variable decimalNumber is not defined in that scope.
// This will cause a ReferenceError.

// 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(500));
11 changes: 9 additions & 2 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

//We need to call function to use it, here will be an error!
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
Expand All @@ -10,11 +10,18 @@ function square(3) {
}

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

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

/*We get a syntax error because `3` is a number,
and function parameters must be valid variable names(x, num etc)*/

// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// -So,I guess the code won't work, because we print the result of multiplication twice

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +10,15 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
//-We need to remove console log in function and use instead of it return

// 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)}`);
*/
11 changes: 10 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

//I believe code won't work because return's value isn't assigned
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +9,14 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
/*
The function returns undefined because return is followed by a line break.
JavaScript ends the return statement immediately, so the line a + b; is never executed.
To fix this, the expression must be placed on the same line as return.
*/
// Finally, correct the code to fix the problem
// =============> write your new code here

function sum(a, b) {
return a + b;
}
29 changes: 28 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here

//-I think here is error in function ,in getLastDigit(.......) should be num in parentheses

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +16,36 @@ 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
/*
So, I was almost right ,
the num is stick to this number "103", and actually it it should be in parenthesis.
*/

// Explain why the output is the way it is
// =============> write your explanation here
/*
// =============> write your explanation here
The function `getLastDigit()` does not accept any parameters,
and always returns the last digit of the constant `num = 103`.
It completely ignores the numbers passed in (42, 105, 806) because it's not using them.
That's why the result is always the same — "3".

The last digit of 42 is 3
VM757:8 The last digit of 105 is 3
VM757:9 The last digit of 806 is 3
*/

// Finally, correct the code to fix the problem
// =============> write your new code here
/*

Choose a reason for hiding this comment

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

This is great but I think you left your code commented out.

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
13 changes: 11 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,14 @@
// 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 bmi = weight / (height * height);
if (bmi <= 18.5) {
return "Underweight";
} else if (bmi <= 25.0) {
return "Normal";
} else if (bmi <= 30.0) {
return "Overweight";
} else {
return "Obese";
}
}
5 changes: 5 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,8 @@
// 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 makeAllCaps(text) {
return text.toUpperCase().split(" ").join("_");
}

Choose a reason for hiding this comment

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

This works functionally but it could be improved regarding it's time and space complexity.

You are creating a new array when you call split() on a string. Imagine the input String was the size of a chapter in "lord of the rings" and you called split(" ") on the string? The array would be very large and use up memory.

You could instead, use a different single method that replaces characters without using split() and join().

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for tip.

console.log(makeAllCaps("Anuar"));
19 changes: 19 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,22 @@
// 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

Choose a reason for hiding this comment

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

The function only logs the result. It should ideally return the formatted string to make it "reusable" in other areas of your program. For example: when using the function in the rest of the program you might want to pass the result into another function instead of just logging it.

function toPounds(amount) {
const penceStringWithoutTrailingP = amount.substring(0, amount.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");
console.log(`£${pounds}.${pence}`);
}

toPounds("500p");
toPounds("300p");
toPounds("1000p");
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 @@ -19,16 +19,26 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

//3 times — once for hours, once for minutes, 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?
// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here

// 0 'totalHours';

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

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

// 1 — this is the value of remainingSeconds, because 61 % 60 = 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

// "01" — because 1 is converted to "1", and padStart adds one leading 0 → "01".
14 changes: 12 additions & 2 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// This is the latest solution to the problem from the prep.
// 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.
// 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 === 0) {
return `12:${minutes} am`;
}

if (hours === 12) {
return `12:${minutes} pm`;
}

if (hours > 12) {
return `${hours - 12}:00 pm`;
const newHours = String(hours - 12).padStart(2, "0");
return `${newHours}:${minutes} pm`;
}
return `${time} am`;
}

Choose a reason for hiding this comment

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

You didn't write any new tests that provide coverage for the issues you found in the code.
Please also include them. In TDD we write tests first and write the code later. It's a common strategy for ensuring requirements and edge-cases are considered before time is spent solving the problem. Your tests should initially all fail before you start working on the code to make them pass.

Expand Down