Skip to content
10 changes: 10 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// The function will not work because the variable str is the input to the function.
// Hence, this will throw an error that says that the variable str is already declared.

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

// =============> write your explanation here
// The error message is 'Uncaught SyntaxError: Identifier 'str' has already been declared.'
// Like what I have explained about, the variable str is the input for the function, so it cannot be declared again with let.
// You need to make a new variable with a new name in order for the function to work.

// =============> write your new code here
function capitalise(str) {
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalisedStr;
}
7 changes: 7 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// It will throw an error because decimalNumber is the input for the function, but below we are trying to declare it again using const.

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

Expand All @@ -15,6 +16,12 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// This function would not run because the variable decimalNumber is declared twice.
// In order for it to work, another variable with a different name must be created.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage (decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// There will be an error because the name of the input variable is not defined and we are just putting a number which will throw 'unexpected number' error.
// and likewise, the variable num is undefined.

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

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

// =============> explain this error message here
// This error happens when numeral is improperly positioned/used
// In this case, when declaring a function we should always first declare what is the name of the input parameter
// (instead of immediately inserting the parameter).

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
9 changes: 9 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Predict and explain first...

// =============> write your prediction here
// My prediction is that the second console log will not print what is intended because in the function, the last line is console.log
// Console.log will return nothing (undefined). Instead, the last line should've been return.
// Using return will make the second console log print the result of the function (instead of undefined).

function multiply(a, b) {
console.log(a * b);
Expand All @@ -11,4 +14,10 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// =============> write your explanation here

// 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)}`);
8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// Return will return nothing since no value is assigned to it.
// The line a + b never runs because a function ends with 'return'.

function sum(a, b) {
return;
Expand All @@ -9,5 +11,11 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The console log will print 'undefined' on the call function.
// 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)}`);
8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The function will not run as intended because the parameter is not set
// Instead it always uses the variable num, so it will always only return that variable's last digit.

const num = 103;

Expand All @@ -15,10 +17,16 @@ 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
// All outputs are "3", which is the last digit of the variable num, 103.
// Explain why the output is the way it is
// =============> write your explanation here
// It is because no parameter is set on the function and it instead always uses the variable num.
// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(number) {
return number.toString().slice(-1);
}


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
5 changes: 4 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// 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
const squaredHeight = height ** 2;
const dividedWeight = weight / squaredHeight;
const bmi = dividedWeight.toFixed(1);
return bmi;
}
4 changes: 4 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,7 @@
// 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(string) {
return string.split(" ").join("_").toUpperCase();
}
20 changes: 20 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,23 @@
// 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 "p"
const penceStringWithoutP = penceString.replace("p", "");

// separate pounds and pence
let pence = penceStringWithoutP.slice(-2).padStart(2, "0");
let pounds = penceStringWithoutP.slice(0, -2).padStart(1, "0");

// convert to number and format with commas
pounds = Number(pounds).toLocaleString('en-GB');

return `£${pounds}.${pence}`;
}

// examples
console.log(toPounds("5p")); // £0.05
console.log(toPounds("99p")); // £0.99
console.log(toPounds("1234p")); // £12.34
console.log(toPounds("1234567p")); // £12,345.67
25 changes: 20 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,33 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// 3 times

// 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 the variable TotalHours
// totalHours = (totalMinutes - remainingMinutes) / 60
// totalMinutes = (seconds - remainingSeconds) / 60
// remainingMinutes = totalMinutes % 60
// remainingSeconds = seconds % 60 = 61 % 60 = 1
// totalMinutes = (61 - 1) / 60 = 1
// remainingMinutes = 1 % 60 = 1
// totalHours = (1 - 1) / 60 = 0
// So, the value assigned to num when pad is called for the first time is 0.

// 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".
// First, 0 is turned into string --> "0".
// Then, a pad of 0 is added at the start so the string length will be 2.
// Hence, the return value is "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 is the variable remainingSeconds.
// As calculated above, remainingSeconds = 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
// The return value of pad when it is called for the last time is "01".
// First, 1 is turned into string --> "1".
// Then, a pad of 0 is added at the start so the string length will be 2.
// Hence, the return value is "01".
50 changes: 46 additions & 4 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,42 @@
// Make sure to do the prep before you do the coursework
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.

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

// Observation:
// the minutes are hardcoded for hours > 12
// incorrect midnight (00:00) it will return 00:00 am instead of 12:00 am
// incorrect noon (12:00) it will return 12:00 am instead of 12:00 pm
// no padding for single digit hours

// Fixing the function:
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
let hours = Number(time.slice(0, 2)); // get hours as number
const minutes = time.slice(2); // keep the minutes string
let suffix = "am";

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

// pad single-digit hours with leading zero
const hoursStr = String(hours).padStart(2, "0");

return `${hoursStr}${minutes} ${suffix}`;
}


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

const currentOutput3 = formatAs12HourClock("00:00");
const targetOutput3 = "12:00 am";
console.assert(
currentOutput3 === targetOutput3,
`current output: ${currentOutput3}, target output: ${targetOutput3}`
);

const currentOutput4 = formatAs12HourClock("12:00");
const targetOutput4 = "12:00 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);