Skip to content
19 changes: 15 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
// the error message details: SyntaxError: Identifier 'str' has already been declared
// In function definition the variable str is already declared, therefore in the function body
// a new declaration of the same variable is redundant.
// when a function call is made, variable str gets the value from the argument of the call.
// In order to fix the issue, we can easily just return the value as it is given in line 8.
// =============> write your new code here

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

console.log(capitalise("hello, world! tom's here."));
30 changes: 23 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// The error will occure because of the redeclaration of the variable decimalNumber in the function body.
// I believe there will be another error when this one's fixed. The console.log (decimalNumber) will not work.

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

// =============> write your explanation here
// As said earlier, the error is because of the redeclaration of the variable decimalNumber in the function body.
// The error message details: SyntaxError: Identifier 'decimalNumber' has already been declared
// Once I've coreecte the code, I get the following error:
// ReferenceError: decimalNumber is not defined
// This is because the variable decimalNumber is not declared in the global scope, but only in the function scope.
// Being 0.5 the value we want to apply the function to, we could just use it in the function call.

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

return percentage;
}

console.log(decimalNumber);

// =============> write your explanation here

// Finally, correct the code to fix the problem
// =============> write your new code here
console.log(convertToPercentage(0.5));
16 changes: 12 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@

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

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

// =============> write your prediction of the error here
// there should be an error, because instead of the parameter, that is function variable, we are passing a value (number 3).
//

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

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

// =============> explain this error message here
// The interpreter is expecting a variable name, but instead we are passing a value 3.
// We can fix this by changing the parameter to a variable name, and then passing the value 3 in the function call.

// Finally, correct the code to fix the problem

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

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

console.log(square(3));
20 changes: 15 additions & 5 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...

// =============> write your prediction here
//
// In the console.log of the function call, the result of multiplication will not be logged, but instead the undefined value will be logged.

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

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

// =============> write your explanation here

// The result of the multiplication is being logged but from within the function, not from the console.log of the function call which gives the result of undefined.
// The reason for this is that the function is not returning anything, therefore the console.log is not giving the result of the multiplication.
// To fix this, instead of console.log in the function, we can use return to return the result of the multiplication.
// 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)}`);
22 changes: 15 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here

// return statement doesn't have a value. so the returned value will be undefined.
// a + b never gets executed, and if it did, it wouldn't give any result because it only calculates the sum, it doesn't return it or save it anywhere.
// function sum(a, b) {
// return;
// a + b;
// }
//
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
//
// =============> write your explanation here
// The output is: The sum of 10 and 32 is undefined
// This is because the return statement doesn't have a value, so the returned value is undefined.
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return;
a + b;
return a + b;
}

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

// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here
34 changes: 23 additions & 11 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
// Predict and explain first...

// Predict the output of the following code:
// The function getLastDigit should return the last digit of the number.
// But, it doesn't work properly because it doesn't take any parameters, and the input is always the same, and that is the value of the variable num (103), which is defined in the global scope.
// So, the function should always return the last digit of the number, which is 3, whatever the input value (the argument) is.
// =============> Write your prediction here

const num = 103;

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)}`);
// const num = 103;
//
// 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)}`);

// 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
// The function getLastDigit doesn't take any parameters, so it doesn't matter what the input value is, it will always return the last digit of the num, defined in the global scope, so the result is always 3.
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
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)}`);
11 changes: 9 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@
// 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
}
// return the BMI of someone based off their weight and height
const bmi = Math.round((weight / (height * height)) * 10) / 10;

return bmi;
}

console.log(calculateBMI(70, 1.73)); // Output: 23.4
console.log(calculateBMI(80, 1.8)); // Output: 24.7
console.log(calculateBMI(60, 1.6)); // 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

function toUpperSnakeCase(inputString) {
// return the input string in UPPER_SNAKE_CASE
const upperSnakeCase = inputString.toUpperCase().replace(/ /g, "_");

return upperSnakeCase;
}

console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS"
22 changes: 22 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,25 @@
// 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}`;
}
console.log(toPounds("399p")); // Output: £3.99
console.log(toPounds("5p")); // Output: £0.05
console.log(toPounds("1234p")); // Output: £12.34
5 changes: 5 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// It will be called 3 times, once for each of totalHours, remainingMinutes, and 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
// The value assigned to num when pad is called for the first time is 0, which is the value of totalHours when seconds is 61.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad when it is called for the first time is "00". Since num is 0, the while loop adds a "0" to the front of numString until its length is 2, resulting in "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
// The value assigned to num when pad is called for the last time in this program is 1, which is the value of remainingSeconds when seconds is 61. This is because 61 seconds is equal to 1 minute and 1 second, so remainingSeconds is calculated as 61 % 60, which equals 1.

// e) What is the return value of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// The return value of pad when it is called for the last time in this program is "01". Since num is 1, the while loop adds a "0" to the front of numString until its length is 2, resulting in "01".
42 changes: 27 additions & 15 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
if (hours === 0 || hours === 24) {
return `12:${time.slice(3, 5)} am`;
} else if (hours > 12) {
return `${(hours - 12).toString().padStart(2, "0")}:${time.slice(3, 5)} pm`;
} else if (hours < 12) {
return `${time.slice(0, 2)}:${time.slice(3, 5)} am`;
} else {
return `${time.slice(0, 2)}:${time.slice(3, 5)} pm`;
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
//
const testInputOutputPairs = [
{ input: "08:00", expectedOutput: "08:00 am" },
{ input: "23:00", expectedOutput: "11:00 pm" },
{ input: "12:00", expectedOutput: "12:00 pm" },
{ input: "13:00", expectedOutput: "01:00 pm" },
{ input: "00:00", expectedOutput: "12:00 am" },
{ input: "24:00", expectedOutput: "12:00 am" },
{ input: "01:17", expectedOutput: "01:17 am" },
{ input: "21:20", expectedOutput: "09:20 pm" },
];

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);
for (const { input, expectedOutput } of testInputOutputPairs) {
const currentOutput = formatAs12HourClock(input);
const targetOutput = expectedOutput;
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
}
Loading