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
17 changes: 11 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// The function is supposed to convert the first character of a string to uppercase
// There will be a syntax error due to the redeclaration of the variable 'str'

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
capitalise("hello");

// function capitalise(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

// The error occurred because 'str' was declared again with 'let', which is not allowed.

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
return `${str[0].toUpperCase()}${str.slice(1)}`;
}

// =============> write your explanation here
// =============> write your new code here
24 changes: 15 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// Predict and explain first...
// The function is intended to take a decimal number and convert it to a percentage (e.g. 1.5 => 150%)

// Why will an error occur when this program runs?
// =============> write your prediction here
// There will be a syntax error due to the redeclaration of the variable 'decimalNumber'

// 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
// Redeclaring the variable decimalNumber causes a syntax error
// The parameter decimalNumber cannot be logged to the console as it is a parameter and holds no value until the function is called

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

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

// 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
// A number is not a valid parameter (Syntax Error)

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

// =============> write the error message here
// Failed to instrument Sprint-2/1-key-errors/2.js
// 7 |
// > 8 | function square(3) {
// | ^ SyntaxError: Unexpected token (8:16)
// 9 | return num * num;
// 10 | }
// 11 |
// at <rootDir>/Sprint-2/1-key-errors/2.js:8:16

// =============> explain this error message here
// parameter names cannot start with a digit

// Finally, correct the code to fix the problem

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

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

console.log(square(3));
19 changes: 12 additions & 7 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// The result of a * b will be printed to the console
// Since the function does not return any value, it will return undefined

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
// Because the function is evaluated before the string is printed to the console, it will log the result of a * b to the console first, but because it does not return a value, the next console.log of the string will print 'The result of multiplying 10 and 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(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
19 changes: 12 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// The function is intended to take 2 arguments and return the sum
// it will return undefined

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

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

// it currently has a return statement that ends the function prematurely, causing it to return undefined
// Finally, correct the code to fix the problem
function sum(a, b) {
return;
a + b;
return 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
// =============> write your new code here
37 changes: 24 additions & 13 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
// Predict and explain first...
// The function intends to take a number and return it's last digit
// Each call of the function will return the same result

// Predict the output of the following code:
// =============> Write your prediction here
// 'The last digit of 42 is 3'
// 'The last digit of 105 is 3'
// 'The last digit of 806 is 3'

const num = 103;
// 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
// Because the function was not correctly defined to accept an argument, It was using a fixed variable instead

// 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
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)}`);
6 changes: 4 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,7 @@
// 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 (weight / height ** 2).toFixed(1);
}
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of value do you expect the function to return? A number or a string?
Does your function return the type of value you expect?


console.log(calculateBMI(70, 1.73));
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(string) {
return string.replace(/\s+/g, "_").toUpperCase();
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
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) {
penceString = penceString.endsWith("p")
? penceString.slice(0, -1)
: penceString;

const paddedString = penceString.padStart(3, "0");

const pounds = paddedString.slice(0, -2);

const pence = paddedString.slice(paddedString.length - 2);

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

// Example calls
console.log(toPounds("399p"));
console.log(toPounds("5p"));
console.log(toPounds("123"));
console.log(toPounds("4567"));
13 changes: 8 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,21 @@ 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:
console.log(formatTimeDisplay(61));

// 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 call to pad is for remaining seconds, which is 1 (since 61 % 60 = 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"
// pad(1) returns "01" because padStart(2, "0") adds a leading zero
77 changes: 75 additions & 2 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const minutes = time.slice(3, 5);
if (hours >= 12) {
return `${hours !== 12 ? hours - 12 : hours}:${minutes} pm`;
}
return `${time} am`;
}
Expand All @@ -23,3 +24,75 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

// Midnight
const outMidnight = formatAs12HourClock("00:00");
const targetMidnight = "00:00 am";
Comment on lines +29 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

00:00 should be equivalent to 12:00 am.

console.assert(
outMidnight === targetMidnight,
`current output: ${outMidnight}, target output: ${targetMidnight}`
);

// Noon
const outNoon = formatAs12HourClock("12:00");
const targetNoon = "12:00 pm";
console.assert(
outNoon === targetNoon,
`current output: ${outNoon}, target output: ${targetNoon}`
);

// 1 PM
const out13 = formatAs12HourClock("13:00");
const target13 = "1:00 pm";
console.assert(
out13 === target13,
`current output: ${out13}, target output: ${target13}`
);

// 12:59 PM
const out1259 = formatAs12HourClock("12:59");
const target1259 = "12:59 pm";
console.assert(
out1259 === target1259,
`current output: ${out1259}, target output: ${target1259}`
);

// 11:59 PM
const out2359 = formatAs12HourClock("23:59");
const target2359 = "11:59 pm";
console.assert(
out2359 === target2359,
`current output: ${out2359}, target output: ${target2359}`
);

// 01:00 AM
const out1 = formatAs12HourClock("01:00");
const target1 = "01:00 am";
console.assert(
out1 === target1,
`current output: ${out1}, target output: ${target1}`
);
Comment on lines +69 to +74
Copy link
Contributor

Choose a reason for hiding this comment

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

Some return values are padded with zeroes and some aren't.
It would be better to make the format more consistent.


// 10:30 AM
const out1030 = formatAs12HourClock("10:30");
const target1030 = "10:30 am";
console.assert(
out1030 === target1030,
`current output: ${out1030}, target output: ${target1030}`
);

// 15:45 PM
const out1545 = formatAs12HourClock("15:45");
const target1545 = "3:45 pm";
console.assert(
out1545 === target1545,
`current output: ${out1545}, target output: ${target1545}`
);

// Edge: single digit hour
const outSingle = formatAs12HourClock("7:45");
const targetSingle = "7:45 am";
console.assert(
outSingle === targetSingle,
`current output: ${outSingle}, target output: ${targetSingle}`
);