Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
// }

// console.log(capitalise("lowercase"));

// =============> write your explanation here
// The error states str has already been declared. This is because str is the parameter and within the
// function it is trying to declare a new variable with the same name using let.

// =============> write your new code here
function capitalise(str) {
let newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
return newStr;
}

console.log(capitalise("lowercase"));
23 changes: 17 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// My prediction of the error is that nothing will be displayed because console.log isn't calling a function
// it is just calling a variable that is not defined in this scope.

// 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
// The error occurs because the variable 'decimalNumber' is the parameter of the function
// but it is being re-declared inside the function which causes a conflict.

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

return percentage;
}

console.log(convertToPercentage(0.5));
16 changes: 11 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

// 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
// Prediction of the error is that the function parameter is not valid.
// It should be a variable name, not a number.

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 error message indicates that there is an unexpected number in the function parameter list.

// Finally, correct the code to fix the problem

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


console.log(square(4)); // should print 16, does display correctly.
16 changes: 12 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

// =============> write your prediction here
// The function multiply does not return any value, it only logs the result to the console.

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 function multiply does not return any value, it only logs the result to the console.
// When we try to use the result of multiply(10, 32), it will be undefined.

// 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)}`);
18 changes: 13 additions & 5 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
// The function sum does not return any value because of the misplaced semicolon after return.

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

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

// =============> write your explanation here
// The function sum has a return statement followed by a semicolon, which causes the function to return undefined.

// 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)}`);
28 changes: 22 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

// Predict the output of the following code:
// =============> Write your prediction here
// The function will always return '3' because it uses the global variable num instead of the input parameter.

const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
}
// 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)}`);
// 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 does not use its input parameter. Instead it always converts the global
// variable num, which is 103 to a string and returns its last character which is '3'.

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

function getLastDigit(number) {
return number.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
10 changes: 8 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,11 @@
// 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
}
let heightSquared = height * height;
let bmi = weight / heightSquared;
return bmi.toFixed(1);
}

console.log(calculateBMI(70, 1.73));
// Expected output: 23.4
// Actual 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) {
let upperCaseString = inputString.toUpperCase();
let upperSnakeCaseString = upperCaseString.replace(/ /g, "_");
return upperSnakeCaseString;
}

console.log(toUpperSnakeCase("lord of the rings"));
// Expected output: "LORD_OF_THE_RINGS"
// Actual output: "LORD_OF_THE_RINGS"
23 changes: 23 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,26 @@
// 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(penceStringWithP) {
const penceStringWithoutTrailingP = penceStringWithP.substring(
0,
penceStringWithP.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("50p")); // Exp: £0.50, Acc: £0.50
console.log(toPounds("399p")); // Exp: £3.99, Acc: £3.99
console.log(toPounds("5p")); // Exp: £0.05, Acc: £0.05
console.log(toPounds("12345p")); // Exp: £123.45, Acc: £123.45
7 changes: 7 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ 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
// The 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
// The value assigned to num is 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// 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 is 1 because the last call to pad is for the remaining seconds, which is 1 second.

// 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 is "01" because the last call to pad is for the remaining seconds, which is 1 second.
39 changes: 38 additions & 1 deletion Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
// 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`;
// }

// Updated function:
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(3, 5);

if (hours === 0) {
return `12:${minutes} am`;
}

if (hours === 12) {
return `12:${minutes} pm`;
}

if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${hours - 12}:${minutes} pm`;
}

return `${time} am`;
}

Expand All @@ -23,3 +43,20 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Morning times
console.assert(formatAs12HourClock("01:00") === "01:00 am");
console.assert(formatAs12HourClock("11:59") === "11:59 am");

// Noon
console.assert(formatAs12HourClock("12:00") === "12:00 pm");
console.assert(formatAs12HourClock("12:30") === "12:30 pm");

// Afternoon / evening
console.assert(formatAs12HourClock("13:00") === "1:00 pm");
console.assert(formatAs12HourClock("18:45") === "6:45 pm");
console.assert(formatAs12HourClock("23:59") === "11:59 pm");

// Midnight
console.assert(formatAs12HourClock("00:00") === "12:00 am");
console.assert(formatAs12HourClock("00:15") === "12:15 am");