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 2 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
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
24 changes: 17 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,29 @@

// 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%"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also execute the following statement to see if it produce the output you expected?

console.log(convertToPercentage(0.1));

Copy link
Author

Choose a reason for hiding this comment

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

it gives me the same result, because the decimalNumber was declared above so the program ignores the (0.1).
I have now removed the value, so it takes what I have wrote down in thee console.log

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
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
// 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
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
// =============> write your new code here
38 changes: 31 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,47 @@

// 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 was not accepting any parameters.