Skip to content

London | May 2025 | Halimatou Saddiyaa | Module-Structuring-and-Testing-Data | Sprint 2 #575

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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

// I think the code will not rum properly because let in line 10 should be declared before the function in line 9 is called.

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

Expand All @@ -10,4 +12,14 @@ function capitalise(str) {
}

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

// The error is: Uncaught SyntaxError: Identifier 'str' has already been declared. It means that we cannot declare a variable with the same name as a parameter inside the function. In this case, We don't need the declaration let as str is already declared in the function's parameter.

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

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

console.log(capitalise("hello world"));
12 changes: 12 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,8 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

//I think an error will occur when this program runs because the variable decimalNumber is declared in line 10 in the parameter of the function and on line 11 as a constant.

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

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

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

// The error is: Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared. This means that we cannot declare a variable with the same name as a parameter inside the function. In this case, we don't need the declaration const as decimalNumber is already declared in the function's parameter.

// 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(0.5));
10 changes: 10 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
//The error will probably be related to the digit 3 in the function because 3 is not considered as a number in javascript. We need to explicitly declare it as a number for the code to run.

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

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

// =============> explain this error message here
//The error message means that we cannot use a digit number as a parameter name in a function. In this case, we need to use a valid variable name that Javascript recognises like num instead of the digit number 3.

// Finally, correct the code to fix the problem

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

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

console.log(square(3));



11 changes: 11 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

// I think the error will probably be because the function is not followed by a return statement.

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

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

// The message printed is "The result of multiplying 10 and 32 is undefined". As the function multiply does not have a return statement, console.log cannot print the result of the multiplication.
// To allow the function to return the result of the multiplication, we need to add a return statement inside the function.

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

// I think the error will probably be because the return statement is not correctly placed in the function.

function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +11,15 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

// The message printed is "The sum of 10 and 32 is undefined". As the expression of the return statement is placed in a new line, it is not being executed.
// To allow the function to return the result of the sum, we need to place the expression in the same line as the return statement.

// 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)}`);
19 changes: 19 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Predict the output of the following code:
// =============> Write your prediction here

// I think the error will probably be because num as been defined as a constant with a value. So the function will only return the output with the constant num as a value.

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +17,27 @@ 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 output is:
// 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

// The output is like this because the function parameter is not being used. So, the function is using the constant num with a value of 103. That's why the last digit of each number is always 3, which is the last digit of 103.

// 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
9 changes: 7 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,10 @@
// 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
}
// 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.

What type of value do you expect the function to return? A number or a string?
Does your function return the type of value you expect it to return?

Copy link
Author

Choose a reason for hiding this comment

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

I was expecting the value to return a number, but after checking with the typeof operator, I can see that it returns a string. I need to use the parsefloat function to make sure that the string converts to a number. I've corrected it on my code.

}

console.log(calculateBMI(70, 1.73));
7 changes: 7 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,10 @@
// 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(input) {
return input.toUpperCase().replaceAll(" ", "_");
}

console.log(convertToUpperSnakeCase("hello there"));
console.log(convertToUpperSnakeCase("lord of the rings"));
24 changes: 24 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,27 @@
// 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("8p"));
console.log(toPounds("77p"));
console.log(toPounds("1007p"));
7 changes: 7 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,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
// pad will be called 3 times, one for totalHours, one for remainingMinutes and one for remainingSeconds

// 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 because it is calling for totalHours which is 0 in this case.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// It is '00' because the function converts the number 0 to a string and pads it with zeros to make sure it is at least 2 characters long.

// 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 because it is calling for remainingSeconds which is 1 in this case.

// 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
// It is '01' because the function converts the number 1 to a string and pads it with zeros to make sure it is at least 2 characters long.