Skip to content

Sheffield | May-2025 | WALEED-YAHYA SALIH-TAHA | Sprint-2 #625

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 15 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 Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

17 changes: 14 additions & 3 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
// The code will throw an error because the variable 'str' is being redeclared with 'let' inside the function,
// which is not allowed since 'str' is already defined as a parameter of the function.

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

// =============> write your explanation here
// The error occurs because the variable 'str' is being declared again with 'let' inside the function,
// which conflicts with the parameter 'str' that is already defined. In JavaScript, you cannot redeclare a variable with 'let' in the same scope.
// =============> write your new code here
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello")); // Output: "Hello"


// =============> write your explanation here
// =============> write your new code here
25 changes: 18 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// The code will throw an error because the variable 'decimalNumber' is being redeclared with 'const' inside the function,
// which is not allowed since 'decimalNumber' is already defined as a parameter of 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;
}
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

console.log(decimalNumber);
// return percentage;
// }
// console.log(decimalNumber);

// =============> write your explanation here
// The error occurs because the variable 'decimalNumber' is being declared again with 'const' inside the function,
// which conflicts with the parameter 'decimalNumber' that is already defined. In JavaScript,

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
// decimalNumber = 0.5; // Remove 'const' to avoid redeclaration
const percentage = `${decimalNumber * 100}%`;

return percentage;
}
console.log(convertToPercentage(0.5)); // Output: "50%"
console.log(convertToPercentage(0.1)); // Output: "10%"
15 changes: 12 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// The code will throw an error because the function is trying to use a number literal (3) as a parameter name,
// which is not allowed in JavaScript. Function parameters must be valid identifiers, and a number

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

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

// =============> explain this error message here
// The error message indicates that there is a syntax error because the function parameter cannot be a number.
// Function parameters must be valid identifiers, and using a number as a parameter name is not allowed

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}
console.log(square(3)); // Output: 9


19 changes: 15 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Predict and explain first...

// =============> write your prediction here
// This code will not produce the expected string output. Instead, it will:
// Log the result of the multiplication directly (320)
// Then print:The result of multiplying 10 and 32 is undefined

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
// multiply(10, 32) runs inside the template literal.
// Inside the multiply function, console.log(a * b) prints 320 to the console.
// However, the multiply function does not return a value → so it returns undefined by default.

// Finally, correct the code to fix the problem
//You should return the result from the function instead of logging it directly:
function multiply(a, b) {
return a * b;
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// =============> write your new code here
24 changes: 17 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// This code will output:
// The sum of 10 and 32 is undefined
// because the sum function does not return a value.
// 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
// The sum function does not return a value because the return statement is immediately followed by a semicolon.
// This means the function exits before it can execute the a + b expression.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your new code here
function sum(a, b) {
return a + b; // Corrected to return the sum of a and b
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // Output: The sum of 10 and 32 is 42

45 changes: 38 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,54 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The last digit of 42 is undefined
// The last digit of 105 is undefined
// The last digit of 806 is undefined

const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// const num = 103;

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

// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is undefined
// The last digit of 105 is undefined
// The last digit of 806 is undefined
// Explain why the output is the way it is
// =============> write your explanation here
// The output is undefined because the getLastDigit function does not accept any parameters.
// It always uses the global variable num, which is set to 103.
// The function getLastDigit should take a number as an argument and return its last digit.

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

function getLastDigit(num) {
return num.toString().slice(-1);
}


// This program should tell the user the last digit of each number.
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)}`);

// Explain why getLastDigit is not working properly - correct the problem
// =============> write your explanation here
// The getLastDigit function is not working properly because it does not accept any parameters.
// It should take a number as an argument and return its last digit.
// The corrected function now accepts a number as an argument and returns its last digit correctly.
// The function now works as intended, returning the last digit of each number passed to it.
// The output will now be:
// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 6


16 changes: 15 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
if (height <= 0) {
throw new Error("Height must be greater than zero.");
}
if (weight <= 0) {
throw new Error("Weight must be greater than zero.");
}
const bmi = weight / (height * height);
return parseFloat(bmi.toFixed(1)); // Return BMI to 1 decimal place
}
// Example usage:
const weight = 70; // in kg
const height = 1.73; // in metres
const bmi = calculateBMI(weight, height);
console.log(`Your BMI is: ${bmi}`); // Output: Your BMI is: 23.4

17 changes: 17 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@
// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
function toUpperSnakeCase(input) {
// Check if input is a string
if (typeof input !== 'string') {
throw new Error("Input must be a string.");
}
// Convert the string to uppercase and replace spaces with underscores
return input.toUpperCase().replace(/ /g, '_');
}
// Example usage:
const inputString = "hello there";
const upperSnakeCaseString = toUpperSnakeCase(inputString);
console.log(upperSnakeCaseString); // Output: "HELLO_THERE"

// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

const inputString1 = "LORD_OF_THE_RINGS";
const upperSnakeCaseString1 = toUpperSnakeCase(inputString1);
console.log(upperSnakeCaseString1); // Output: "LORD_OF_THE_RINGS"


// 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
21 changes: 20 additions & 1 deletion Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,24 @@

// You will need to take this code and turn it into a reusable block of code.
// 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) {
// Remove the trailing "p"
const penceStringWithoutTrailingP = penceString.slice(0, -1);

// Pad the string so it's at least 3 digits
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Extract pounds and pence
const pounds = paddedPenceNumberString.slice(0, -2);
const pence = paddedPenceNumberString.slice(-2).padEnd(2, "0");

return `£${pounds}.${pence}`;
}

// Test cases
console.log(toPounds("399p")); // £3.99
console.log(toPounds("99p")); // £0.99
console.log(toPounds("9p")); // £0.09
console.log(toPounds("1234p")); // £12.34
16 changes: 13 additions & 3 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function pad(num) {
return num.toString().padStart(2, "0");
}
console.log(num);
return num.toString().padStart(2, "0");
}

function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
Expand All @@ -9,7 +10,7 @@ function formatTimeDisplay(seconds) {
const totalHours = (totalMinutes - remainingMinutes) / 60;

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
}

// 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
Expand All @@ -18,17 +19,26 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// The function formatTimeDisplay will call pad three times: once for totalHours, once for remainingMinutes, and once 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
// The value assigned to num when pad is called for the first time is 0.
// The first call to pad is for totalHours, which is 1.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when called for the first time is "01". This is because pad converts the number 1 to a string and pads it with a leading zero to ensure it has at least two digits.

// 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
// The value assigned to num when pad is called for the last time is 1. This is because remainingSeconds is calculated as 61 % 60, which equals 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
// The return value assigned to num when pad is called for the last time is "01".
// This is because pad converts the number 1 to a string and pads it with a leading zero to ensure it has at least two digits.
// The final output of formatTimeDisplay(61) will be "00:01:01", where each component is padded to two digits.
console.log(formatTimeDisplay(61)); // Output: "00:01:01"
Loading