Skip to content
11 changes: 9 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> Error due to "let str", str already exists due to being created by "capitalise(str)"

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -8,6 +8,13 @@ function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello world"));

// =============> write your explanation here
// =============> The error spells it out "SyntaxError: Identifier 'str' has already been declared"
// =============> write your new code here

function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("hello world"));
14 changes: 12 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> Similar to previous js, "const decimalNumber" already exists due to "convertToPercentage(decimalNumber)"

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

Expand All @@ -14,7 +14,17 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> "const decimalNumber = 0.5;" should come before the function...? However I didn't notice another problem if I did that until I tried it was console.log is going to just grab the 0.5 that has nothing done to it, skipping the function. To correct this I changed it to "console.log(convertToPercentage(decimalNumber))"

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

const decimalNumber = 0.5;

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

return percentage;
}

console.log(convertToPercentage(decimalNumber));
11 changes: 7 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

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

// =============> write your prediction of the error here
// =============> I am not sure but I do not think that (3) should be there in that way....

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

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

// =============> explain this error message here
// =============> It doesn't like there being a number in "function square(3)" I also noticed obviously that return of num was not being told what num was anywhere at all.

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(50))
11 changes: 9 additions & 2 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

// =============> write your prediction here
// =============> Why are there 2 console.logs? I am not sure how to explain but I don't think it will work because of that.

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

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

// =============> write your explanation here
// =============> Well nothing is being returned so the outside console.log is just printing undefined. I will change the first console.log into a const and return that for the function.

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

function multiply(a, b) {
const result = (a * b);
return result;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10 changes: 8 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> Return being by it self and just ending with a ; is definitely just going to return nothing.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,12 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> Return is not returning the a + b because it is cut off by the ; and on a different line.
// Finally, correct the code to fix the problem
// =============> write your new code here

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
16 changes: 13 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> All the console.logs will say "3" because of "const num = 103;" is going to be where they will all grab from because "function getLastDigit()" is empty instead of num.

const num = 103;

Expand All @@ -14,11 +14,21 @@ 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
// =============> All the console.logs are outputting "3" as the answer.
// Explain why the output is the way it is
// =============> write your explanation here
// =============> Because in the function getLastDigit only .slice(-1) is functioning and it is taking the num value from "const num = 103;"
// 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
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
}
const bmi = weight / (height ** 2);
return Number(bmi.toFixed(1));
}

console.log("Your BMI is: " + 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 snake(text) {
return text.toUpperCase().replaceAll(" ", "_");
}

console.log(snake("hello there"))
console.log(snake("lord of the rings"))
22 changes: 22 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,25 @@
// 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("10449p"));
12 changes: 7 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ 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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3, in "return `${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
// =============> 0, there are no hours in 61 seconds. (first is totalHours)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00", as there are 0 hours it pads it to 00 at least.

// 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, as it is 1 over 60 and the 60 gets turned into remainingMinutes

// 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", the last call is "${pad(remainingSeconds)}" which is the previous 1 padded into 01.