Skip to content
13 changes: 13 additions & 0 deletions Sprint-2/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

// I predict that the function will capitalise the first character of the string
// And then return a string using string interpolation using the first character of the string
// and a slice of the remaining characters of the string excluding the first character.

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

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

// An error is thrown because of the variable name collision. The function has a parameter called `str`
// but in the body of the function, a variable `str` is declared and that conflicts with the parameter.

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

function upperCased(string) {
const capitalised = `${string[0].toUpperCase()}${string.slice(1)}`;
return capitalised;
}
9 changes: 9 additions & 0 deletions Sprint-2/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

// Because of the variable name collision. The function parameter is called decimalNumber
// but another decimalNumber is declared inside the body of the function.

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

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

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

// The console prints: SyntaxError: Identifier 'decimalNumber' has already been declared

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

function percentaged(decimalNumber) {
return `${decimalNumber * 100}%`;
}
12 changes: 11 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@

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

// The function will not execute because of the undefined variable `num` and the parameter `3`,
// which is not allowed to be defined with just numbers. There must be letters to define a variable
// but can begins with numbers.

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

// =============> write the error message here

// SyntaxError: Unexpected number

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

// It's referring to the function parameter `3`

// Finally, correct the code to fix the problem

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


function squared(value) {
return value * value;
}
9 changes: 9 additions & 0 deletions Sprint-2/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 function prints to the console by multiplying the parameter `a` and `b`

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

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

// I missed line 11, the function does indeed prints to the console but returns nothing
// which explains `undefined` in the string interpolation of the function

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

function product(a, b) {
return a * b;
}
9 changes: 9 additions & 0 deletions Sprint-2/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 not say `undefined` but will not add parameter `a` and `b`

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

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

// The console prints: The sum of 10 and 32 is undefined

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

function add(a, b) {
return a + b;
}
10 changes: 8 additions & 2 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// Predict the output of the following code:
// =============> Write your prediction here

// It will convert 103 number into a String representation and then will either do no manipulations or invert the string

const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
function getLastDigit(digit) {
return digit.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
Expand All @@ -22,3 +24,7 @@ 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 function getLastDigit() has no parameters and referenced a globally scoped constant `num`.
// In order for it to `work properly`, a parameter is required and referencing it in order to
// return an expected value
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
}
// return the BMI of someone based off their weight and height
const result = weight / Math.pow(height, 2);
const bmiString = result.toFixed(1);
return Number(bmiString);
}
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(string).toUpperCase().replaceAll(" ", "_");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If string is already a string, we don't have to convert it to a string before using the string methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true but it didn't show up in the auto-complete, also given that JavaScript is a dynamically typed language without type safety, the parameter is any type, so I need to ensure that I am dealing with a String type otherwise it fails.

}
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) {
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}`;
}
13 changes: 13 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

const number = 61;
console.log(formatTimeDisplay(number));

// 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 @@ -19,16 +22,26 @@ function formatTimeDisplay(seconds) {
// 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

// "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"

// 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"
43 changes: 39 additions & 4 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
// 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`;
let hours = Number(time.slice(0, 2));
const minutes = time.slice(-2);

if (hours === 24) {
hours = 0;
}
return `${time} am`;

const period = hours < 12 ? "am" : "pm";
hours = hours % 12 || 12;
const paddedHours = String(hours).padStart(2, "0");

return `${paddedHours}:${minutes} ${period}`;
}

const currentOutput = formatAs12HourClock("08:00");
Expand All @@ -23,3 +30,31 @@ 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("24:00");
const targetOutput4 = "12:00 am";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

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

const currentOutput6 = formatAs12HourClock("09:41");
const targetOutput6 = "09:41 am";
console.assert(
currentOutput6 === targetOutput6,
`current output: ${currentOutput6}, target output: ${targetOutput6}`
);