Skip to content

Sheffield | May-2025 | Declan Williams | Sprint-2 #565

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 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
4cb9354
fix: resolve variable redeclaration error in capitalize function
CatchingKiyoko Jun 9, 2025
2fafe6e
fix: comment out erroneous capitalize function to prevent redeclarati…
CatchingKiyoko Jun 9, 2025
7620265
fix: correct parameter name in square function to prevent syntax erro…
CatchingKiyoko Jun 13, 2025
071cf21
fix: add console log for square function with example input and corre…
CatchingKiyoko Jun 13, 2025
c1c09a6
update comments to clarify expected output and explanation in getLast…
CatchingKiyoko Jun 13, 2025
e72b13a
fix: correct getLastDigit function to accept parameters and return th…
CatchingKiyoko Jun 13, 2025
832d9bb
fix: update explanation for getLastDigit function to clarify paramete…
CatchingKiyoko Jun 13, 2025
2a18e2c
explained in comments why errors occured and then refactored the code…
CatchingKiyoko Jun 14, 2025
dde5917
fix: update multiply function to return the product instead of loggin…
CatchingKiyoko Jun 15, 2025
fdf492e
fix: correct sum function to return the sum of two numbers and update…
CatchingKiyoko Jun 15, 2025
48d2e3a
feat: implement BMI calculation function and add example usage
CatchingKiyoko Jun 15, 2025
e2c65bf
feat: implement toUpperSnakeCase function to convert strings to UPPER…
CatchingKiyoko Jun 16, 2025
bb731c1
feat: implement toPounds function to convert pence strings to pounds …
CatchingKiyoko Jun 17, 2025
8c166ff
fix: add console log and comments for clarity in formatTimeDisplay fu…
CatchingKiyoko Jun 17, 2025
2f41d57
Update 1-bmi.js
CatchingKiyoko Jun 28, 2025
d04b7bd
Update 2-cases.js
CatchingKiyoko Jun 28, 2025
b860c0c
Merge branch 'CodeYourFuture:main' into coursework-Sprint-2
CatchingKiyoko Jul 12, 2025
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
16 changes: 14 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here
// I expect the code to possibly throw an error because str is being declared twice. once in the function and again in the function body declared as "let"
// This function looks like its trying to turn the first letter of a string into a capital letter.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// after trying to run the code it produced a "SyntaxError: Identifier 'str' has already been declared"

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

console.log(capitalise("hello")); */
// =============> write your explanation here
// this is because the variable str is declared twice, once in the function parameters and again inside the function body with "let str"
// str is declared as a parameter, so we don't need to declare it again with let you can just use it directly.
// to fix the error, we can remove the "let" keyword from the second declaration of str.
// =============> write your new code here

function capitalize(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalize("the first letter should be capitalized"));
15 changes: 14 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I expect a Syntax error to be thrown due to the const "decimalNumber" has already been declaired in the parameters of the function.
// I also expect an error from the "console.log(decimalnumber)" as its calling "decimalNumber" from outside 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;
}

console.log(decimalNumber);
console.log(decimalNumber);
*/

// =============> write your explanation here
// When running the code i get thrown a Syntax error: "identifer 'decimalNumber' has already been declared". To solve this you must remove the "const decimalNumber = 0.5" from the function as its already been defined in the parameters of the function.
// after testing the code and fixing the syntax error we then recived another error: "ReferenceError: decimalNumber is not defined" To solve this you must change the "console.log(decimalNumber)" to "console.log(convertToPercentage())".

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

// =============> write your prediction of the error here
// The function is defined incorrectly using a number as a parameter name so it will throw an error when called because looking for an identifier but finds a number instead.

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

// =============> write the error message here
/* function square(3) {
^

SyntaxError: Unexpected number
*/
// =============> explain this error message here
// SyntaxError: This means there is something wrong with the way the code is written, so JavaScript cannot understand it.
// unexpected number: This means that the code is trying to use a number where it expects an identifier (like a variable name). In this case, the function parameter is incorrectly named as a number (3) instead of a valid identifier.

// Finally, correct the code to fix the problem
// to fix the problem, we need to change the parameter name to a valid identifier, like "num" or "number".

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


function square(num){
return num * num;
}
console.log(square(3));
console.log(square(10));
14 changes: 12 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Predict and explain first...
// I expect the code to log the results of multiplying 10 and 32 but in line 10 i expect it to return undefined because the function "multiply" does not have a return statement.

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

/*
function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

*/
// =============> write your explanation here
// The code defines a function "multiply" that takes two parameters "a" and "b" and logs their product to the console. However, it does not return any value from the function.
// The function `multiply` is called with arguments 10 and 32, but since it does not return a value, the template literal will log "undefined" for the result of the multiplication.
// and to fix this, the function should return the product instead of just logging it.


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

function multiply(a, b){
return a * b; // Return the product instead of logging it
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // now this will log the correct results
8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I expect the code to log the sum of 10 and 32, but it will return undefined because the function "sum" does not have a return statement for the sum operation as line 7 has unreachable code due to the return statement on line 6 is returning nothing.

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

// =============> write your explanation here
// The code defines a function "sum" that is intended to return the sum of two numbers "a" and "b". However, the return statement is placed before the addition operation, which means that the function will return `undefined` immediately without performing the addition. As a result, when we log the output, it will show "The sum of 10 and 32 is undefined".
// Finally, correct the code to fix the problem
// =============> write your new code here
// To fix this we need to moving the unreachable code after the return statement into the return statement itself, so that the function returns the sum of "a" and "b".

function sum(a, b){
return a + b;
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // This will now correctly log "The sum of 10 and 32 is 42".
20 changes: 18 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

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

// I expect each of the calls to return the last digit of "num" which is "3" its defined as a constant and we are using that const in the return statement.
/*
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +13,28 @@ function getLastDigit() {
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
// Output:
// 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 the same for all three calls because the function getLastDigit does not take any parameters. It always uses the constant "num" which is defined as 103, and will always returns the last digit of 103, which is "3".

// 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
// we corrected the problem by adding a parameter to the function getLastDigit, so it now takes a number as an argument and returns the last digit of that number.
8 changes: 7 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,10 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const heightSquared = height * height; // square the height
const bmi = weight / heightSquared; // calculate BMI by dividing weight by height squared
const roundedBmi = bmi.toFixed(1); // round the result to 1 decimal place
return roundedBmi; // return the rounded BMI value
}
console.log(calculateBMI(70, 1.73)); // Example usage: should return 23.4
console.log(calculateBMI(80, 1.8)); // Example usage: should return 24.7
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 toUpperSnakeCase(input){
const lowerCaseString = input.toUpperCase(); // converts the string input into uppercase(Capital letters)
const snakeCaseString = lowerCaseString.replace(/ /g, "_") // replaces the spaces with underscores
return snakeCaseString; // returns the string in UPPER_SNAKE_CASE
}
console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
13 changes: 13 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,16 @@
// 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}p`;
}

console.log(toPounds("399p"));
console.log(toPounds("100p"));
console.log(toPounds("7688p"));
console.log(toPounds("1p"));
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 @@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(61)); // Expected output: "00:01:01"

// 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,23 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
//Answer: it is called 3 times once for each of the three calls to pad in the formatTimeDisplay function in line 11 return statement.

// 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
// Answer: The first call to pad is for totalHours which is 0. because 61 seconds is less than 3600 seconds (1 hour) the totalHours will be 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// Answer: The first call to pad will return "00" because it converts the number 0 to a string and pads it with leading zeros to ensure 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
// Answer: The last call to pad is for remainingSeconds which is 1. The value assigned to num will be 1 because it is the remaining seconds after calculating total hours and total minutes.

// 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
// Answer: The last call to pad will return "01" because it converts the number 1 to a string and pads it with leading zeros to ensure it is at least 2 characters long.