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
11 changes: 8 additions & 3 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
// =============> there are two declarations of the variable str.

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

// =============> write your explanation here
// =============> write your new code here
// =============> str is declared twice in the function capitalise and using let.

// =============> we delete the 'let'
function capitalise(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
11 changes: 9 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
// =============> The variable decimalNumber has been declared twice and will retun an error.

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

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

console.log(decimalNumber);

// =============> write your explanation here
// =============> The variable decimalNumber has been declared twice and returned an error saying it had already been declared. There is also a console log error as it is trying to use a variable that is inside the function and hasn't been declared outside.


// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
console.log(percentage);
return percentage;
}
//We can also use console log inside the function.
9 changes: 6 additions & 3 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
// =============> the error message will be about 'num' not being declared.

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

// =============> write the error message here
// =============> Uncaught SyntaxError: Illegal return statement.

// =============> explain this error message here
// =============> The error message is because this num is not a valid variable as it has been undeclared.

// Finally, correct the code to fix the problem

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


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

// =============> write your prediction here
// =============> there is no return statement, therefore, you cannot use the result of the function in a console log. The function will run but it will return undefined.

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
// =============> you need to add a return statement in order to use the result and so it can be displayed on the console.

// 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,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> return statement is on a new line, therefore there will be an error.

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

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

// =============> write your explanation here
// =============> return statement should be on the same 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)}`);
13 changes: 10 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
// =============> num has already been declared as 103, therefore, no other number can be used.

const num = 103;

Expand All @@ -14,11 +14,18 @@ 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 is always 3 because no other inputs are allowed.
// Explain why the output is the way it is
// =============> write your explanation here
// =============> No other inputs are allowed as num will always be defined as 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
1 change: 1 addition & 0 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
return (weight / (height * height)).toFixed(1);
// return the BMI of someone based off their weight and height
}
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 upperSnakeCase(sentence) {
const upperCase = sentence.toUpperCase();
const snakeCase = upperCase.replaceAll(" ", "_");
return snakeCase;
}
console.log(upperSnakeCase("hello there"));
8 changes: 8 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,11 @@
// 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"));
11 changes: 6 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times, once for totalHours, once for remainingMinutes and once for 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
// =============> 1 because the first operation is 61 % 60 which gives us 1

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 00 because the first operation is totalHours which is 0 and it is padded to 2 digits with a 0 in front of it.

// 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 because the last operation is 1 % 60 which gives us 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 because the last operation on the return line is remainingSeconds which is 1 and it is padded to 2 digits with a 0 in front of it.