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
12 changes: 12 additions & 0 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Predict and explain first...
// =============> write your prediction here

// Prediction: the method toUpperCase return the character at index 0 of str as uppercase.
// slice method will return the characters of str starting at index 1 excluding the first character.
// the template literal combines the converted upper case first character and the rest of the string.

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

Expand All @@ -10,4 +14,12 @@ function capitalise(str) {
}

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

// Explanation: 1. str declares twice.Hence it gives syntax error.
// Refactor: 2. instead of returning str return can directly send back the template literal expression.

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

function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
14 changes: 14 additions & 0 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// Prediction:
// 1. decimalNumber is declared twice and it will give syntax error.

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

function convertToPercentage(decimalNumber) {
Expand All @@ -16,5 +19,16 @@ console.log(decimalNumber);

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

// 2. console.log will return undefined as decimalNumber local scope declaration inside the function.
// 3. It's percentage not decimalNumber needs printing.

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

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

console.log(convertToPercentage(0.5));

// The function convertToPercentage can be called passing with an argument of the input.
15 changes: 15 additions & 0 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@

// =============> write your prediction of the error here

// Prediction:
// 1. This will give reference error.
//


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

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

// =============> explain this error message here

// The function expects a parameter name and it shouldn't start with a number.
// num should be the parameter and later 3 can be passed as an argument.

// Finally, correct the code to fix the problem

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

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

console(square(3));

10 changes: 10 additions & 0 deletions Sprint-3/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// =============> write your prediction here

// 'The result of multiplying 10 and 32 is 10 * 32' is printed.

function multiply(a, b) {
console.log(a * b);
}
Expand All @@ -10,5 +12,13 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

// the function prints a * b. When presented with multiply(10, 32) it prints 10 * 32.

// 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)}`);
15 changes: 14 additions & 1 deletion Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Predict and explain first...
// =============> write your prediction here

// it will return syntax error

function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +11,16 @@ function sum(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

// There is a semicolon after return.

// Finally, correct the code to fix the problem.
// Later I discovered having a line between return and a + b gives another undefined error message
// =============> write your new code here

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

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

25 changes: 25 additions & 0 deletions Sprint-3/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// Predict the output of the following code:
// =============> Write your prediction here

// The last digit of 42 is 3 undefined
// The last digit of 105 is 3 undefined
// The last digit of 806 is 3 undefined

const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +19,31 @@ 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 the function is not given a parameter and it is being passed an argument the same time which it ignores.
// The function uses the global variable num instead and print 3

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

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

// The reason was the num variable wasn't declared as a parameter inside the function.
// At the same time the function is receiving arguments which the function ignores.
5 changes: 3 additions & 2 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// Then when we call this function with the weight and height
// It should return a string of their Body Mass Index to 1 decimal place


function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / (height * height)).toFixed(1);
}
4 changes: 4 additions & 0 deletions Sprint-3/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(str) {
return str.toUpperCase().replaceAll(" ", "_");
}
13 changes: 13 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
// 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 toPound(priceInPence) {
const penceDigits = priceInPence
.substring(0, priceInPence.length - 1)
.padStart(3, "0");

const poundsPart = penceDigits.substring(0, penceDigits.length - 2);

const pencePart = penceDigits.substring(penceDigits.length - 2);

return `£${poundsPart}.${pencePart}`;
}
console.log(toPound("399p"));
12 changes: 12 additions & 0 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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
Expand All @@ -23,16 +24,27 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// pad is 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 that assigned to num is 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here

// The return value of pad 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. This is because 1 is the last character in the string 61

// 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 is "01". when pad is called for the last time it takes the last character of the string 61.
// After checking remainingSeconds length which is less than 2 it adds a "0" and returns "01"
79 changes: 70 additions & 9 deletions Sprint-3/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,83 @@

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`;
return `${time} AM`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
const currentOutput1 = formatAs12HourClock("00:00");
const targetOutput1 = "12:00 AM";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
currentOutput1 === targetOutput1,
`current output 1: ${currentOutput1}, target output: ${targetOutput1}`,
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
const currentOutput2 = formatAs12HourClock("00:01");
const targetOutput2 = "12:01 AM";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output 2: ${currentOutput2}, target output: ${targetOutput2}`,
);

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

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

const currentOutput5 = formatAs12HourClock("12:01");
const targetOutput5 = "12:01 PM";
console.assert(
currentOutput5 === targetOutput5,
`current output 5: ${currentOutput5}, target output: ${targetOutput5}`,
);

const currentOutput6 = formatAs12HourClock("13:00");
const targetOutput6 = "1:00 PM";
console.assert(
currentOutput6 === targetOutput6,
`current output 6: ${currentOutput6}, target output: ${targetOutput6}`,
);

const currentOutput7 = formatAs12HourClock("23:00");
const targetOutput7 = "11:00 PM";
console.assert(
currentOutput7 === targetOutput7,
`current output 7: ${currentOutput7}, target output: ${targetOutput7}`,
);

const currentOutput8 = formatAs12HourClock("23:59");
const targetOutput8 = "11:59 PM";
console.assert(
currentOutput8 === targetOutput8,
`current output 8: ${currentOutput8}, target output: ${targetOutput8}`,
);

// Edge test cases tested for:

// 1) 00:00 -> 12:00 AM
// 2) 00:01 -> 12:01 AM
// 3) 08:00 -> 08:00 am
// 4) 12:00 -> 12:00 PM
// 5) 12:01 -> 12:01 PM
// 6) 13:00 -> 1:00 PM
// 7) 23:00 -> 11:00 PM
// 8) 23:59 -> 11:59 PM
Loading