Skip to content
7 changes: 7 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
//the function is there for capitalise the given string

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +10,11 @@ function capitalise(str) {
return str;
}

console.log(capitalise("hello"))
// =============> write your explanation here
// Identifier 'str' has already been declared - means the variable already exist - with the let you don't need to declare this again
// =============> write your new code here
//function capitalise(str) {
// str = `${str[0].toUpperCase()}${str.slice(1)}`;
//return str;
//}
14 changes: 14 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
//the function is there to convert the given number to percentage

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

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

// =============> write your explanation here
//the function is there to convert the given number to percentage
//the error will come up again as the decimalNumber has already been declared - we don't need to assign this as a variable again
//the console log missing the assigned number with the brackets
//also we would need to call the function not the parameter


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

//return percentage;
//}

//console.log(convertToPercentage(decimalNumber));
8 changes: 8 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

// Predict and explain first BEFORE you run any code...
//function is there to calculate the square parameter

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

Expand All @@ -10,11 +11,18 @@ function square(3) {
}

// =============> write the error message here
// we are returning the num - but the parameter already given as an argument not a parameter 3 -
// for the function to recognize num we would need to give this to the function as a parameter

// =============> explain this error message here
//unexpected number - the function has an unexpected argument

// Finally, correct the code to fix the problem

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


9 changes: 8 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
//function is there to multiply the given arguments in the console log line 10


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

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

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

// we would have an error message as we would need to return the statement
// 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)}`);
9 changes: 7 additions & 2 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 will sum the given parameters

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
// unable to return nothing - the statement is not given - has to be in the same line with the return
// Finally, correct the code to fix the problem

// =============> write your new code here
// function sum(a, b) {
//return a + b;
//}
19 changes: 18 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Predict and explain first...
// the function should get the last digit of the given parameter -
// but the parameter is the already existing variable

// Predict the output of the following code:
// =============> Write your prediction here
//already assigned variable num will be returned as we not defined the parameter inside the return


const num = 103;

function getLastDigit() {

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

Expand All @@ -15,10 +20,22 @@ 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
//every time we call the function we return the same number as this is already assigned inside line 10
// Explain why the output is the way it is
// =============> write your explanation here
//we need to give the parameter to the function to be able to use the arguments in the console log
// Finally, correct the code to fix the problem
// =============> write your new code here
//const num = 103;


//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
4 changes: 3 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
return (weight / Math.pow(height, 2)).toFixed(1)
}
console.log(calculateBMI(56, 1.60))
5 changes: 5 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

// Implement a function that:

function upperSnakeCase(srt){
return srt.toUpperCase().replaceAll(" ", "_")
}
console.log(upperSnakeCase("hello there"))

// Given a string input like "hello there"
// When we call this function with the input string
// it returns the string in UPPER_SNAKE_CASE, so "HELLO_THERE"
Expand Down
26 changes: 26 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,29 @@
// 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 convertToPound(srt) {
const penceStringWithoutTrailingP = srt.substring(0, srt.length - 1);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

return pounds;
}

function convertToPence(srt) {
const penceStringWithoutTrailingP = srt.substring(0, srt.length - 1);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

return pence;
}

console.log(`£${convertToPound("399p")}.${convertToPence("399p")}`);
16 changes: 11 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,36 @@ function pad(num) {
function formatTimeDisplay(seconds) {
const remainingSeconds = seconds % 60;
const totalMinutes = (seconds - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

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 function formatTImeDisplay calls 3 times the pad function -pad(totalHours) pad(remainingMinutes) pad(remainingSeconds)

// 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 number 0 = which is the calculation of const totalHours = (totalMinutes - remainingMinutes) / 60;

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00" = the function pad(-receives the number 0 or the value of totalHours) and converts this to a string =
// than the while loop kicks in and runs while the conditions is true and exit once it returns as false
// numString = "0" + numString; this line will add a a "0" to our new string if our condition is true - (numString.length < 2)

// 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
// =============> number 1 - the calculation of const remainingSeconds = seconds % 60;

// 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
// =============> "01" pad(1) - converts to a string "1" and adds the "0" + with the while loop to our newly created string
47 changes: 40 additions & 7 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
// This is the latest solution to the problem from the prep.
// 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 pad(num) {
let numString = num.toString();
while (numString.length < 2) {
numString = "0" + numString;
}
return numString;
}

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 === 0) {
return `12:${minutes} am`;
} else if (hours > 12) {
return `${pad(hours - 12)}:${minutes} pm`;
} else if (hours === 12) {
return `12:${minutes} pm`;
}
return `${time} am`;
return `${pad(hours)}:${minutes} am`;
}

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

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
const currentOutput2 = formatAs12HourClock("12:00");
const targetOutput2 = "12:00 pm";
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("22:59");
const targetOutput4 = "10:59 pm";
console.assert(
currentOutput4 === targetOutput4,
`current output: ${currentOutput4}, target output: ${targetOutput4}`
);

const currentOutput5 = formatAs12HourClock("21:30");
const targetOutput5 = "09:30 pm";
console.assert(
currentOutput5 === targetOutput5,
`current output: ${currentOutput5}, target output: ${targetOutput5}`
);
Loading