Skip to content
8 changes: 7 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// I think this function will capitalise a string

// 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)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

// =============> write your explanation here
// the variable has already been declared when it was passed to the function
//
// =============> write your new code here

const my_string = "hello world";
console.log(capitalise(my_string));
7 changes: 5 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I think the value "decimalNumber" is undeclared so it will not work

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

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

return percentage;
}

console.log(decimalNumber);
let decimalNumber = 0.5;
console.log(convertToPercentage(decimalNumber));

// =============> write your explanation here
// The variable decimalNumber didn't need to be redefined in the function
// Instead it should come from outside and be passed to the function

// Finally, correct the code to fix the problem
// =============> write your new code here
9 changes: 5 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@

// 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
// I think it's because we're not passing in the argument properly

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

// =============> write the error message here
// SyntaxError: Unexpected token `numeric literal (3, 3)`. Expected yield, an identifier, [ or {

// =============> explain this error message here
// It means that we're putting 3 but it should be an identifier, such as "num"

// Finally, correct the code to fix the problem

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


// replaced 3 with num
5 changes: 4 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Predict and explain first...

// =============> write your prediction here
// I think the function will not work because nothing is returned

function multiply(a, b) {
console.log(a * b);
return a * b;
}

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

// =============> write your explanation here
// I was almost correct but I had to also remove the console.log()

// Finally, correct the code to fix the problem
// =============> write your new code here
// 10: return a * b;
5 changes: 3 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Predict and explain first...
// =============> write your prediction here
// It won't run because there's a semicolon straight after return

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
// remove the semicolon
14 changes: 8 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

// Predict the output of the following code:
// =============> Write your prediction here
// I think it wont run because there's nothing passed into the function
// also it is using the value of num outside of the function wich isn't correct

const num = 103;

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

Expand All @@ -14,11 +14,13 @@ 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
// =============> it just returned 3

// Explain why the output is the way it is
// =============> write your explanation here
// =============> Because value is mistakenly defined outside of the function

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> pass num into the function and remove the constant 103 outside

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
8 changes: 6 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,9 @@
// 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 height_squared = Math.pow(height, 2);
return (weight / height_squared).toFixed(1);
}

console.log(calculateBMI(80, 1.8));
6 changes: 6 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,9 @@
// 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 to_snake(str) {
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(to_snake("lord of the rings"));
21 changes: 21 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,24 @@
// 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}`;
}

console.log(toPounds("399p"));
14 changes: 9 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ function formatTimeDisplay(seconds) {
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

console.log(remainingSeconds);

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(60001));

// 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
// =============> 3

// 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 is totalHours which equals to (totalMinutes - remainingMinutes) / 60

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

// 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 of pad when it is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> 01
Loading