Skip to content
Open
11 changes: 8 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> This code will give an error message

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +9,10 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
// =============> it gave an error because the variable str is being declared twice one with the function and another one with anew variable
// =============> this can be fixed by changing the variable name inside the function
function capitalise(str) {
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalised;
}
console.log(capitalise("sophia")); // Output: "Sophia"
17 changes: 14 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> I can see that the variable decimalNumber is being declared twice once as a function parameter and again as a constant

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

Expand All @@ -14,7 +14,18 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> so the function is to convert a decimal number into a percentage
// so if the number is 0.5 it is multiplied by 100
// % sign is added at the end to make it 50%

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> the problem can be fixed by removing the redeclaration of the variable decimalNumber inside the function
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

// Call the function and store the result in a variable
const result = convertToPercentage(0.5);
console.log(result);

19 changes: 15 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@

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

// =============> write your prediction of the error here
// =============> First there is a number inside the function which is not allowed in JavaScript it should be a variable name
//the second mistake is num is not declared

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

// =============> write the error message here
// =============> Uncaught SyntaxError: Unexpected number
// =============> we put a number (3)where JavaScript expected a variable name
//Uncaught SyntaxError: Illegal return statement

// =============> explain this error message here
//we cant use return inside the function without declaring the function properly

// Finally, correct the code to fix the problem

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

// =============>
function square(num) {
return num * num;
}
console.log(square(3)); // the answer should be 9
console.log(square(5)); // the answer should be 25
console.log(square(10)); // the answer should be 100



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

// =============> write your prediction here
// =============> console.log will print the result because log()will write the message to the console.
//

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

// =============> When testing the code in Node.js, the output was 320 without the message because it was undefined "The result of multiplying 10 and 32 is undefined".
//we need to use return to store the result then print it out.
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> // Corrected Code:
function multiply(a, b) {
return a * b; // Use 'return' instead of 'console.log'
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

10 changes: 7 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> i think there will be and error message because at doesn't show what value to return so it will be undefined.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,10 @@ function sum(a, b) {

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

// =============> write your explanation here
// ============= it should be in the same line after return statement to return the value
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> corrected code:
function sum(a, b) {
return a + b;
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
18 changes: 13 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> I predict that the code might not work because I am declaring num outside the function also I am giving num a constant value means it will always be 103

const num = 103;

Expand All @@ -14,11 +14,19 @@ 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 code worked but the answer was 3 i predicted an error message.
// Explain why the output is the way it is
// =============> write your explanation here
// =============> the output was 3 because at the beginning it we declared that num value as 103
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> corrected code is running the code without giving num any value and add num in the function

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
// Explain why getLastDigit is not working properly - it was not working at the start because every time we run the code would slice the last number of the 103 because we declared num as a constant with value 103
17 changes: 14 additions & 3 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
// Then when we call this function with the weight and height
// 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
}
// I have made a small change to the test cases to ensure they match the expected output to 1 decimal place
function calculateBMI(weight, height) {
// Calculate BMI using the formula: weight (kg) / (height (m) * height (m))
const bmi = weight / (height * height);

// Round to 1 decimal place and return
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.

Both of these function calls output 123 in the console, but internally in the program,
the number 123 and the string "123" are stored and treated differently.

  console.log(123);
  console.log("123");

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

Copy link
Author

Choose a reason for hiding this comment

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

I expect the function to return a number, because a BMI is a numeric value. Yes, I tested the function, and it comes as a number.

Copy link
Contributor

Choose a reason for hiding this comment

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

How did you test if its return value is a number and not a string?

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot 2025-10-26 185042

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not a proper way to check the type of a value.

Can you ask AI (and then verify if needed) how to determine the type of a value in JS?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much, that was really helpful. I used (type of )and it showed me that my output was a string, and I changed it to a number

}

// Test cases
console.log(calculateBMI(85, 1.54)); // 35.8
console.log(calculateBMI(60, 1.65)); // 22.0
console.log(calculateBMI(72, 1.80)); // 22.2
// now the function is reusable and correct.
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

// There is 2 things we need to do: change the space to_ and change the letters from lowercase to uppercase

function toUpperSnakeCase(str) {
return str
.replaceAll(' ', '_') // replace spaces with underscores
.toUpperCase(); // make everything uppercase
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
10 changes: 10 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,13 @@
// 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(kg) {
const pounds = kg * 2.20462;
return `${pounds.toFixed(2)} lbs`; // returns a string
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still not what is expected.

Copy link
Author

Choose a reason for hiding this comment

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

I have changed the code, and the output is a number, not a string

Copy link
Contributor

Choose a reason for hiding this comment

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

This function is supposed to convert pence to British pounds (similar to what you did in Sprint-1/3-mandatory-interpret/3-to-pounds.js

Copy link
Author

Choose a reason for hiding this comment

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

I think this question is to convert the weight from kilograms to Pounds, not British pounds

Copy link
Contributor

Choose a reason for hiding this comment

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

What makes you think that? What does the comments (lines 1-60 in the file say?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you so much for all your help

}

// Test cases
console.log(toPounds(1)); //the output "2.20 lbs"
console.log(toPounds(5)); //the output is "11.02 lbs"
console.log(toPounds(10)); //the output is "22.05 lbs"
14 changes: 9 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 Times
pad(totalHours)
pad(remainingMinutes)
pad(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
// =============> 0 first value is totalHours

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00" because the target length is 2 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
// =============> 1 this is last value is remaining in Seconds

// 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
// =============> 01 because the target length is 2 digits