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
12 changes: 11 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// =============> It should take a string and capitalise the first letter but i predict it will throw an

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

/*The error occurs because str is declared twice. The function already receives str as a parameter, and then let str tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring a variable with let, so it throws an error.*/

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

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

/* This function takes a string and capitalise the first letter. It uses `str[0].toUpperCase()` to convert the first character to uppercase and `str.slice(1)` to get the rest of the word unchanged. The function then joins both parts together and returns the new string. When `console.log(capitalise("hello"), capitalise("world"))` is run, the function executes twice and prints `Hello World`.
*/

// =============> write your new code here
function capitalise(str) {
return (str = `${str[0].toUpperCase()}${str.slice(1)}`);
}
console.log(capitalise("hello"), capitalise("world"));
11 changes: 10 additions & 1 deletion Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here

Expand All @@ -16,5 +15,15 @@ console.log(decimalNumber);

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

/*An error will occur when the program runs because decimalNumber is declared twice. The function already receives decimalNumber as a parameter, but then const decimalNumber = 0.5 tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring variables with const.
Another error will happen at console.log(decimalNumber) because decimalNumber only exists inside the function and cannot be accessed outside of it.*/

// 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));
7 changes: 7 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
/*An error will occur because a number was used as a function parameter instead of a variable name.*/

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

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

// =============> explain this error message here
/*The error happens because JavaScript expects a variable name inside the function brackets, but it found a number instead.*/

// Finally, correct the code to fix the problem

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

function square(num) {
return num * num
}
console.log(square(3));

8 changes: 8 additions & 0 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Predict and explain first...

// =============> write your prediction here
/*The code will print a correct multiplication inside the function*/

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

// =============> write your explanation here
/*The result shows undefined because the function prints the answer with console.log instead of returning it, so nothing is passed back into the template string.*/

// 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)}`);
11 changes: 10 additions & 1 deletion Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Predict and explain first...
// =============> write your prediction here

//The sum of 10 and 32 is undefined
function sum(a, b) {
return;
a + b;
Expand All @@ -9,5 +9,14 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

/*The function returns undefined because the return; statement ends the function immediately, so the calculation a + b never runs or gets returned.*/

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

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

const num = 103;

Expand All @@ -15,10 +20,28 @@ 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

/*The function always returns the last digit of 103 because it ignores the input parameter and uses a fixed global variable instead of the number passed into it.*/

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

function getLastDigit(number) {
return number.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

/*The function is not working properly because it ignores the input values and always uses the global variable num (which is 103), so every result returns the last digit of 103 instead of the number passed into the function.*/
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
}
const bmi = weight / (height * height);
return Number(bmi.toFixed(1)); // return the BMI of someone based off their weight and height
}
console.log(calculateBMI(80, 1.73));
10 changes: 10 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,13 @@
// 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(str) {
return str
.split(" ")
.map((word) => word.toUpperCase())
.join("_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
15 changes: 15 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,18 @@
// 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 withoutP = penceString.slice(0, -1);

const padded = withoutP.padStart(3, "0");

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

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

console.log(toPounds("399p"));
console.log(toPounds("45p"));
console.log(toPounds("5p"));
6 changes: 6 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ function formatTimeDisplay(seconds) {

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// a) 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
// b) 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// c) "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
// d) 1, because the last time pad is called it is for the remaining seconds which is 1 second in this case

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

// e) "01", because the last time pad is called it is for the remaining seconds which is 1 second in this case, and pad adds a leading zero to make it two digits
Loading