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
9 changes: 8 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,11 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
}
(calculateBMI(weight/hight*hight));
let bmi = 85 / (1.54 * 1.54);
console.log(bmi)
//here the result was 35.833307439446366 I need to round it to 1 decimal place
console.log(bmi.toFixed(1));
//now the output is 35.8
Copy link
Contributor

Choose a reason for hiding this comment

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

The objective of this exercise is to complete the implementation of the function calculateBMI() so that we can reuse the code in the function to calculate BMI for different weight and height.

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 updated the function


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"));
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(kg) {
const pounds = kg * 2.20462;

return pounds;
Copy link
Contributor

Choose a reason for hiding this comment

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

This toPounds() function is supposed to convert a pence string to a string in British pounds format, like 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 have updated the code as required

}
console.log(toPounds(1));
console.log(toPounds(5));
console.log(toPounds(10));
// if I want to round the result to 2 decimal places I can use toFixed(2)
console.log(toPounds(1).toFixed(2));
console.log(toPounds(5).toFixed(2));
console.log(toPounds(10).toFixed(2));
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