Skip to content
10 changes: 8 additions & 2 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
// =============> I think the variable 'str' is repeated twice in string literals.

// 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,11 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> The error occurs because some parts of the code are not allowed in JS.
// The variable 'str' is already declared in line 7 as a parameter
// =============> write your new code here

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`
}
console.log(capitalise("shop"));
9 changes: 7 additions & 2 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
// =============> variable 'decimalNumber' is repeated already declared as a parameter in line 8

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

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

console.log(decimalNumber);

// =============> write your explanation here
// =============> Again, the error is a syntax error.
// The variable 'decimal' is already declared as a parameter

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

function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`
}
9 changes: 6 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

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

// =============> write your prediction of the error here
// =============> The error occurs because we have not defined 'num'

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

// =============> write the error message here
// =============> It is a syntaxError: Enexpected number

// =============> explain this error message here
// =============> In line 8, the number as a parameter is not expected; instead, a variable is expected.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}


10 changes: 8 additions & 2 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
// =============> In line 6, the word return is missing, I assume.

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
// =============> $`{multiply(10, 32)}` is undefined.

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

function multiply(a, b) {
return a * b;
}

console.log(multiply(10, 32));
10 changes: 8 additions & 2 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
// =============> It will give an error; line 5 and 6 should be in one line.

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

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

// =============> write your explanation here
// =============> The output is undefined because return and a + b are in two different lines.
// Finally, correct the code to fix the problem
// =============> write your new code here

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

console.log(sum(10, 32));
16 changes: 13 additions & 3 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
// =============> It will probaby return the last digit as a string.

const num = 103;

Expand All @@ -14,11 +14,21 @@ 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
// =============> Aha, I was wrong. The outputs for all the console.log is 3
// Explain why the output is the way it is
// =============> write your explanation here
// =============> because, the function takes 'const num = 103' as input.
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(num) {
return num.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

// This code should be working better. It takes the last digit of the number as a string
// and at last, change that string into a number;
function getLastDigit(num) {
return Number(num.toString().slice(-1));
}
7 changes: 5 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,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 bmi = weight / (height * height);
return Math.round(bmi * 10) / 10
}

// console.log(calculateBMI(68, 1.7));
7 changes: 7 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,10 @@
// 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(str) {
return str.split(" ").map(item => item.toUpperCase()).join("_");
}

console.log(toUpperSnakeCase("lord of the rings"));
// returns 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

// Method 1
function toPounds(penceString) {
const penceStringWithoutP = penceString.substring(0, penceString.length - 1);
const paddedPenceNumberString = penceStringWithoutP.padStart(3, "0");
const pound = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2);

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

}
console.log(toPounds("447p")); // £4.47



// Method 2
function toPounds(penceString) {
const value = Number(penceString.slice(0, -1)) / 100;
return `£${value.toFixed(2)}`
}

console.log(toPounds("447p")); // £4.47
15 changes: 10 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,29 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));


// 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
// =============> pad() function will be called 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
// =============> 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 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
// the last pad, shows the remaining seconds (61 % 60 = 1), and it is 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
// =============> 01
// The return value assigned to num when pad is called for the last time shows the seconds and it is 1
28 changes: 23 additions & 5 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@
// 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`;
// }

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
let hours = Number(time.slice(0, 2));
let minutes = Number(time.slice(3, 5));
let ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
hours = hours < 10 ? '0'+hours : hours;

return `${hours}:${minutes} ${ampm}`
}

console.log(formatAs12HourClock("00:00"));
console.log(formatAs12HourClock("23:59"));
console.log(formatAs12HourClock("12:00"));
console.log(formatAs12HourClock("21:20"));


const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
Expand Down