Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6b40bf3
0.js answered prediction and logged error returned
AngelaMcLeary Feb 23, 2026
7241ee3
completed 0.js answered remaining questions and corrected the code to…
AngelaMcLeary Feb 23, 2026
b3de302
updated answers in key errors 0.js and 1.js
AngelaMcLeary Feb 23, 2026
ed06a0f
completed 1.js with comments
AngelaMcLeary Feb 23, 2026
3a3fa15
completed 1.js formatted code
AngelaMcLeary Feb 23, 2026
d7ff511
completed 1-key-errors-2.js updated code to work and added answers
AngelaMcLeary Feb 23, 2026
ca5d386
completed 2-mandatory-debug-0.js updated code to work and added answers
AngelaMcLeary Feb 24, 2026
e91663e
added comments
AngelaMcLeary Feb 25, 2026
c9f66f9
2-madatory-debug 1.js completed with explanations
AngelaMcLeary Feb 26, 2026
00271f5
2-madatory-debug 2.js completed with explanations
AngelaMcLeary Feb 26, 2026
e1876d6
2-madatory-implement 1-bmi.js completed with explanations
AngelaMcLeary Feb 26, 2026
ed21c08
2-madatory-implement 2-cases.js written code toUppercase
AngelaMcLeary Feb 26, 2026
e0a3306
2-madatory-implement 2-cases.js updated underscore and completed
AngelaMcLeary Feb 26, 2026
78d96f8
2-madatory-implement 2-time-format.js completed
AngelaMcLeary Feb 27, 2026
77f2ef2
2-madatory-interpret 2-time-format.js completed formatted
AngelaMcLeary Feb 27, 2026
0d05247
4-madatory-interpret 2-time-format.js completed formatted
AngelaMcLeary Feb 27, 2026
20a496c
3-madatory-implement 3-to-pounds.js completed formatted with updated …
AngelaMcLeary Feb 27, 2026
82fccd4
3-mandatory-implement 1.bmi.js updated output to return a number and …
AngelaMcLeary Mar 2, 2026
61a12b3
1-key-errors 1.js updated console log with the right function to prin…
AngelaMcLeary Mar 2, 2026
364b18d
1-key-errors 0.js updated code to return value without declaring the …
AngelaMcLeary Mar 2, 2026
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
39 changes: 35 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
// Predict and explain first...
// =============> write your prediction here
// 1.*Answer
// We have declared the "str" twice.It is already declared
// in this function's parameter, hence causing the conflict.
// Although "let" allows re-assignment of variables, the issue is redeclaration,
// not reassignment.


// call the function capitalise with a string input
// 2.*Answer
// It throws an error detailed below;
// / home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/tempCodeRunnerFile.js:2
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// ^
// SyntaxError: Identifier 'str' has already been declared.


// interpret the error message and figure out why an error is occurring
// 3.*Answer
// The SyntaxError - Something is invalid that is not following
// Javascript rules.
// The identifier - a name used in the code has a problem and it
// lets m know which one in this case "str". It also let's me know
// what the problem is in this case "str" has already been declared.

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

// =============> write your explanation here
//I have re-assigned the "str" variable and it now runs with no errors.
// I also checked with console.log to see the output.

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

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


36 changes: 26 additions & 10 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// 1.*Answer
// We have declared "decimalNumber" twice, which is not allowed.
// It is already declared as a function parameter, causing a conflict.
// console.log(decimalNumber) results in an error because it is outside
// the function scope.It should call console.log(convertToPercentage(decimalNumber));

// 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;
}
//Try playing computer with the example to work out what is going on
// function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

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

// =============> write your explanation here
// 2.*Answer
// It throws an error detailed below;
// /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:12
// const decimalNumber = 0.5;
// ^
// SyntaxError: Identifier 'decimalNumber' has already been declared

// Removed the const declaration and returned the percentage calculation
// using a template literal to append the percent symbol.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}

console.log(convertToPercentage(0.5));//returns 50%
22 changes: 18 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@

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

// this function should square any number but instead we're going to get an error
// This function should square any number but instead we're going to get an error

// =============> write your prediction of the error here

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

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

// =============> write the error message here
// /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:8
// function square(3) {
// ^
// The SyntaxError: Unexpected number

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

// This occurs because the number "3" is used as a function parameter.
// This is not valid syntax. A function parameter must be a valid variable name (an identifier),
// not a number. JavaScript expects an identifier or no parameter at all.
// That's why it throws a syntax error.
//
// Finally, correct the code to fix the problem

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

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

console.log(square(3));
28 changes: 22 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
// Predict and explain first...

// The first console.log prints 320
//It throws an error at console.log.
// =============> 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)}`);
// 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
// It return the following output:
// 320
// The result of multiplying 10 and 32 is undefined

// why?
//the computer reads the values in the second console.log "multiply(10, 32)" and
//the first console.log only prints the value, it does not store it.
//the second console.log outputs the template literals and undefined
// as it has no value to reach.
Comment on lines +17 to +20
Copy link

Choose a reason for hiding this comment

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

In your explanation, you mentioned that the second console.log outputs undefined 'as it has no value to reach.' You are exactly right! The technical term for this in JavaScript is an implicit return. If you don't explicitly tell a function what to return, JavaScript automatically forces it to return undefined behind the scenes. Keep up the great work!

Copy link
Author

Choose a reason for hiding this comment

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

@tee4tao Thank you for the feedback. I will note that for future explanations.
Thank you
Angela.



// 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)}`);
//prints: The result of multiplying 10 and 32 is 320
23 changes: 17 additions & 6 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here

function sum(a, b) {
return;
a + b;
}
// The computer reads the return statement and stops executing the function.
// So the function stops running before it reaches a + b.
// Because of that, nothing is returned,
// and the console.log prints the text and the values are undefined.

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// current output:The sum of 10 and 32 is undefined

// =============> write your explanation here
// Fix the code to make it work:
// function sum(a, b) {
// return a + b;
// }
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

//To make it work, I need to return a + b.
// 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)}`);
44 changes: 37 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
// Predict and explain first...
// The function does not take in any parameters, therefore it
// refers to the global const variable = 103
// So every console.log will print 3 every time. This function is not
// re-useable as it stands.
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

Good point and observation. This function is not re-useable as it stands, functions should ideally be self-contained and reusable.

Copy link
Author

Choose a reason for hiding this comment

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

@tee4tao
Thank you.
Angela.


// Predict the output of the following code:
// =============> Write your prediction here
//output is 3 for every console.log.

const num = 103;
// const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// 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)}`);
// 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
// "/home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/2-mandatory-debug/2.js"
// 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 function refers to the global const variable = 103
// So every console.log returns 3 every time.

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

const num = 103;

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

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// prints: The last digit of 42 is 2
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
//prints: The last digit of 105 is 5
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
//prints: The last digit of 806 is 6

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// now the code works correctly

// I declared "num" as a parameter of the function,
// so the function now uses the value passed into it
// instead of the global variable "num".
12 changes: 10 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place
// bmi = weight / height**2).

// return the BMI of someone based off their weight and height
function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return Number((weight / (height ** 2)).toFixed(1));// returns a number
// return (weight/(Math.pow(height, 2))).toFixed(1);// returns a string
}
console.log(calculateBMI(100, 1.6)); //print 39.1
console.log(calculateBMI(55, 1.6)); //print 21.5

// I used Math.pow to raise the height to the power of 2 and .toFixed
//for the number of decimal places.
11 changes: 11 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,14 @@
// 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 changeToUpperCaseSnake(string) {
return string.toUpperCase().replaceAll(" ", "_");
}

console.log(changeToUpperCaseSnake("hello there"));
console.log(changeToUpperCaseSnake("lord of the rings"));
console.log(changeToUpperCaseSnake("learning to love Javascript"));

// 1.Capitalise = .toUpperCase
// 2.Find blank space = .replaceAll ("", ...)
// 3.replace with _ = ,replaceAll (..., "_")
34 changes: 34 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,37 @@
// 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

//Rewrite this code
// const penceString = "399p";

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

// console.log(`£${pounds}.${pence}`);

//Updated code to make it re-useable:

function toPounds(fromPenceString) {
const penceString = fromPenceString.substring(0, fromPenceString.length -1);
const paddedString = penceString.padStart(3, "0");
const pounds = paddedString.substring(0, paddedString.length -2);
const pence = paddedString.substring(paddedString.length -2).padEnd(2, "0");
return ${pounds}.${pence}`;
}
console.log(toPounds("399p")); // £3.99
console.log(toPounds("45p")); // £0.45
console.log(toPounds("1295p")); // £12.95
console.log(toPounds("5p")); // £0.05
19 changes: 16 additions & 3 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,37 @@ 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
// It will be called 3 times. 1 for hours, 1 for minutes, 1 for seconds.
// return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;

// Call formatTimeDisplay with an input of 61, now answer the following:
// Call formatTimeDisplay with an input of 61, now answer the following:
// [Running] node "/home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/4-mandatory-interpret/time-format.js"
// 00:01:01

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// The value is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value is a string "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

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// The value is 1. When the input is 61, It is evaluating number of remaining seconds,
// which equals 1.
// it is evaluating from left to right.
//
// 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 is a string "01" because also because .padStart has been used to ensure that the
// value displays as a minimum of 2 digits in this case if less than 2 digits add a leading "0".