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
19 changes: 13 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Predict and explain first...
// =============> write your prediction here

// =============> The function is trying to return a string with the only the first character capitalised
// 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)}`;
// console.log(str);
// return str;
// }

// =============> The code is trying to declare a new variable called str, but this name is already in use by the function parameter so it cannot be declared a second time.
// =============> As I am not using the value anywhere else, I chose to not create a new variable

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
capitalise("amaranth");
capitalise("leonardo");
31 changes: 24 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> There are 2 errors that will occur with this function:
// 1. The function is trying to redeclare decimalNumber, but that's not possible.
// 2. console.log() is trying to print decimalNumber, but that's a local variable in the function so it cannot be accessed outside of it.

// 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
// Like in the previous exercise, decimalNumber is the function parameter, so it is already a local variable in the function and cannot be redeclared.
// After fixing the first error, another one would appear:
// Because decimalNumber, and percentage, are both local variables, the console.log outside the function cannot access them, so it would not be able to print anything.
// Additionally, I have a feeling the console.log() was meant to log the function response by calling the function inside of it.
// For this reason, I've decided to hard code the decimal number values in my function calls, inside console.log() I think this makes more sense.

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

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

return percentage;
}

console.log(convertToPercentage(0.5));
console.log(convertToPercentage(1.2));
console.log(convertToPercentage(0.74));
18 changes: 13 additions & 5 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
// We should get something like num is undefined, because we are not declaring it anywhere.

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

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

// =============> explain this error message here
// I missed the parameter error.
// We can use actual values (numbers, strings, etc.) as arguments when we call a function. But the function will receive these values as variables (identifier), so we cannot have a number as parameter.

// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// We are trying to log the function result, but because we are not returning it, we are not going to be able to. I'm not sure what type of error we will get

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
// The console.log() inside the function is able to print a * b, but the console.log outside the function returns "The result of multiplying 10 and 32 is undefined", because, as already mentioned, the called function is not returning anything

// 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)}`);
20 changes: 15 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// We will get either an undefined or an error, because a + b is not on the same line as return

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

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

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

// return marks the end of a function, telling the computer to exit and go back to the global scope. Because the operation a + b is not on the same line as return, the computer won't be able to read it, so the function is returning nothing.

// 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)}`);
30 changes: 23 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@

// Predict the output of the following code:
// =============> Write your prediction here
// getLastDigit is missing a parameter, meaning it only has access to the const num = 103 declared before. So I'm expecting it to return either an error or 3 for each call.

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
// As I was saying, the function declaration has no parameters, so the only num variable that it can access is the one above it, const num = 103.
// Because of this, even if we call the function with different arguments, we are not actually passing them as parameters, so the result will always be the last digit of 103.

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

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
9 changes: 7 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,10 @@
// 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 Number((weight / (height * height)).toFixed(1));
}

console.log(calculateBMI(70, 1.73));
console.log(calculateBMI(74, 1.61));
console.log(calculateBMI(55, 1.68));
console.log(calculateBMI(85, 1.88));
8 changes: 8 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,11 @@
// 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.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("jurassic park"));
console.log(toUpperSnakeCase("on the road"));
console.log(toUpperSnakeCase("parmigiano reggiano"));
25 changes: 25 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,28 @@
// 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
);

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

console.log(toPounds("399p"));
console.log(toPounds("42p"));
console.log(toPounds("137543p"));
console.log(toPounds("2480p"));
console.log(toPounds("5p"));
18 changes: 13 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,32 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(61));
console.log(formatTimeDisplay(6671));
console.log(formatTimeDisplay(832));

// 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 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"
// The first time, pad() is called with totalHours, which value is "0"

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00"
// pad() adds a "0" at the start of the string to make it 2 characters long

// 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 time, pad() is called with remainingSeconds, which value is "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 adds a "0" before "1" to make it 2 characters long