Skip to content
16 changes: 12 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here
// =============> we declare a variable str twice

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
*/
// =============> write your explanation here
// The error occurs because we are trying to declare a variable 'str' inside the function using 'let',
// =============> write your new code here

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

console.log(capitalise("beatriz")); // Output: "Beatriz"
26 changes: 18 additions & 8 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here - will not run because decimalNumber is declared twice

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
//function convertToPercentage(decimalNumber) {
// const decimalNumber = 0.5;
// const percentage = `${decimalNumber * 100}%`;

return percentage;
}
// return percentage;
//}

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

// =============> write your explanation here
// =============> write your explanation here - An error occur because we are declaring the decimalNumber variable twice.
//we declare it in the function and in the first const.
//we are also logging decimalNumber we should log the function

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

function convertToPercentage() {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}
console.log(convertToPercentage());
20 changes: 14 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

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

// =============> write your prediction of the error here
// =============> write your prediction of the error here - I believe that square is not valid because 3 is not a valid variable

function square(3) {
return num * num;
}
//ORIGINAL CODE
//function square(3) {
// return num * num;
//}
//console.log(square(3));

// =============> write the error message here
// =============> write the error message here - SyntaxError: Unexpected number

// =============> explain this error message here
// =============> explain this error message here - the number 3 is not a valid parameter name. It has to be a name and not a number.

// Finally, correct the code to fix the problem

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

function square(num) {
return num * num;
}
console.log(square(38));

//With this code, we can chose any number to square

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

// =============> write your prediction here
// we have a function that multiplies two numbers and logs the result,

function multiply(a, b) {

/*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
//we have the console doing the multiplication and loggin it, but we
//are not returning anything from the funcntion. there's why the output is undefined.

// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
console.log(a * b);
return a * b;
}

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

// Test 1 answer:
// 320
// The result of multiplying 10 and 32 is 320
// Test 2 answer:
// 1053
// The result of multiplying 27 and 39 is 1053
18 changes: 16 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// The function sum is intended to return the sum of two numbers, a and b. there's an error in the return statement.
// And again it's returning undefined because the return statement is divided.
// Ouput is the sum and then undefined

function sum(a, b) {
/*function sum(a, b) {
return;
a + b;
}

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

*/
// =============> write your explanation here
// ERROR:The sum of 10 and 32 is undefined
// the return statement should have been written in one line as "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 59 and 72 is ${sum(59 , 72)}`);

// Test 1 : The sum of 10 and 32 is 42
// Test 2 : The sum of 59 and 72 is 131
34 changes: 32 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
// Predict and explain first...
// The function getLastDigit is intended to return the last digit of a given number.
// We are declaring the given number as a constant variable 'num' with a value of 103.
// However, the function does not take any parameters and always uses the constant 'num'.
// Therefore, when we call getLastDigit with different numbers (42, 105, 806),
// it still returns the last digit of 103, which is '3'.


// Predict the output of the following code:
// Predict the output of the following code:
// =============> Write your prediction here
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

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

function getLastDigit() {
return num.toString().slice(-1);
Expand All @@ -12,13 +21,34 @@ 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
//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
// because we are declaring the num = 103, even if we change the parameter in the console it still considers the num value
// 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)}`);


//NEW OUTPUT
//The last digit of 42 is 2
//The last digit of 105 is 5
//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
7 changes: 6 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,9 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1);
}

/* module.exports = calculateBMI; */

console.log(calculateBMI(70, 1.73)); // Expected output: 23.4
10 changes: 10 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,13 @@
// 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

export function toUpperSnakeCase(inputString) {
// Replace spaces with underscores
const stringWithUnderscores = inputString.replace(/ /g, '_'); // / /g means that is replacing all spaces globally with _
// Convert the string to uppercase
const upperSnakeCaseString = stringWithUnderscores.toUpperCase();
return upperSnakeCaseString;
}
console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
32 changes: 32 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,35 @@
// 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) {
// Remove the trailing 'p' from the input string
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// Pad with leading zeros to ensure we have at least 3 digits
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// Extract pounds (all digits except the last two)
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// Extract pence (last two digits) and ensure it has exactly 2 digits
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// Return the formatted result
return `£${pounds}.${pence}`;
}

// Tested the function with different inputs
console.log(toPounds("399p")); // £3.99
console.log(toPounds("199p")); // £1.99
console.log(toPounds("50p")); // £0.50
console.log(toPounds("5p")); // £0.05
console.log(toPounds("1234p")); // £12.34
19 changes: 14 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@ 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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 - for hours, minutes and seconds.

// 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 (totalHours = 0 when input is 61 seconds)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00" (0 padded to 2 digits becomes "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
// =============> 1 (remainingSeconds = 61 % 60 = 1)

// 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
// =============> return value is "01".
/*Explanation:
num = 1 (remainingSeconds)

num.toString() converts 1 to "1"

.padStart(2, "0") pads "1" to 2 digits with leading zeros

*/
27 changes: 23 additions & 4 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = time.slice(3, 5); // Get the minutes part

let period = "am";
let displayHours = hours;

if (hours === 0) {
displayHours = 12; // midnight
} else if (hours === 12) {
period = "pm";
} else if (hours > 12) {
displayHours = hours - 12;
period = "pm";
}
return `${time} am`;

return `${displayHours}:${minutes} ${period}`;
}

// Test cases
const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const targetOutput = "8:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
Expand All @@ -23,3 +35,10 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

console.log("If no errors were logged, all tests passed!");

// Export correctly - choose ONE of these:
module.exports = formatAs12HourClock; // Direct export
// OR if you want named exports:
// module.exports = { formatAs12HourClock };