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
9 changes: 4 additions & 5 deletions Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Predict and explain first...
// =============> write your prediction here
// Prediction: I think the code will have a SyntaxError because 'str' is declared twice.

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

// =============> write your explanation here
// =============> write your new code here
// Explanation: The function already has something called str. You can't make a second thing with the same name inside the same function
// New code: removed the 'let str =' line and returned the expression directly.
16 changes: 9 additions & 7 deletions Sprint-3/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Predict and explain first...

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

// Why will an error occur when this program runs
// When this program runs there will be an identifier SyntaxError because 'decimalNumber' has already been declared
// Also the output will print a decimal since it says console.log(decimalNumber);
// 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);
console.log(convertToPercentage(0.5));

// =============> write your explanation here
// 'decimalNumber' is already declared in two different areas in the same function which JavaScript does not allow.
// 'decimalNumber' is only useful inside the function, so it will give an error of not knowing what decimalNumber is,
// even if it succeeds it will give the wrong output since what we are looking for is the percentage.

// Finally, correct the code to fix the problem
// =============> write your new code here
// New code: 1. Removed the duplicate `const decimalNumber = 0.5;` line.
// 2. Changed the console.log to call the function and print the result.
11 changes: 6 additions & 5 deletions Sprint-3/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

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

// =============> write your prediction of the error here
// Prediction: there is going to be an error because there is a number in the parameter

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

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

// =============> explain this error message here
// the parameter of a function must be a name /identifier, not a number.
// JavaScript expected a word as a parameter but fond a number instead hence the SyntaxError.

// Finally, correct the code to fix the problem

// =============> write your new code here
// New code : replaced the parameter with a valid identifier


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

// =============> write your prediction here
// Prediction:I think it will print 320, but the template string might not show the right result, but will return nothing.

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
// because console.log will print whatever it is given .
// the function has no return statement, so when we run the code it will say undefined because there is nothing to give back.

// Finally, correct the code to fix the problem
// =============> write your new code here
10 changes: 5 additions & 5 deletions Sprint-3/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Predict and explain first...
// =============> write your prediction here
// =====> It will not run and will come out as undefined

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
// =====> There is a semicolon right after return, which tells it to stop there,
// so basically anything that comes after that will not run hence the undefined result.
// Finally, correct the code to fix the problem will be to remove the semicolon.
// =============> write your new code here
20 changes: 16 additions & 4 deletions Sprint-3/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
// =============> I think the result prints will always be 3 as num will always be 103.

const num = 103;

Expand All @@ -14,11 +14,23 @@ 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
// =============> because the code is using the outer variable num which is always 103.
// so every call is returning 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
// because there was no parameter so the function has no way of receiving the numbers. so adding a parameter fixes it.
4 changes: 3 additions & 1 deletion Sprint-3/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 a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
return (weight / (height*height)). toFixed(1);
}

console.log(calculateBMI(70, 1.73));
6 changes: 6 additions & 0 deletions Sprint-3/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 toUpperSnakeCase(str) {
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
10 changes: 10 additions & 0 deletions Sprint-3/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
// 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 pence = Number(penceString.replace("p", ""));
return `£${(pence / 100).toFixed(2)}`;
}

console.log(toPounds("399p"));
console.log(toPounds("5p"));
console.log(toPounds("50p"));
console.log(toPounds("1250p"));
console.log(toPounds("0p"));
11 changes: 6 additions & 5 deletions Sprint-3/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 3 times because the return statement has three pad() calls in it. Hrs, Mins, Secs.

// 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 first pad call pad(totalHours) worked out to 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> The value is 00, add another because every pad wants 2 digits.

// 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
// =============> the last pad call receives 1 as its num because last call called is pad(remainingSeconds) which is calculated as seconds % 60.
// and with the seconds being 61, that's 61 % 60 giving 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
// =============> The call is pad(1) and the pad adds a 0 to make the string 2 digits
Loading