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
13 changes: 10 additions & 3 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
// =============> I believe an error will occur as the same variable name has been 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;
}
};

// =============> write your explanation here
// =============> When running the code there was an error message saying 'Identifier 'str' has already been declared.
// This lets us know that the identifier - which is the name of the variable str - has been declared more than once. The error occurs as the same variable name - str - is being declared twice which is not allowed.
// I would personally fix this by changing the variable name of the second declaration.
// I tested this and didn't get a capitalised result - I realised this was because I hadn't also changed the variable name after 'return' - this was still str so I changed it to capitalised and now it works.
// =============> write your new code here
function capitalise(str) {
let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`;
return capitalised;
};
14 changes: 11 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> There are two errors I notice here. The first is that, similarly to the first exercise, the variable decimalNumber is being declared twice - first as the parameter of the function and then again with const decimalNumber.
// JavaScript does not allow for a variable to be declared more than once.
// The second error is the console.log section where it attempts to access a variable inside the function which will not work. The function is what needs to be called.

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

// =============> write your explanation here
// =============> As the decimalNumber variable has already been declared within the function I removed the second const declaration as it is unnecessary.
// I changed the console.log section to call the function and not the variable. I then tested this and got the expected result.

// 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.3));
15 changes: 10 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

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

// =============> write your prediction of the error here

// =============> The first error I see is the number 3 as the parameter inside the function. This is the error that JavaScript will flag as the parameter needs to be a variable name.
// The second error I noticed is num + num - what is num? JavaScript will not know as it has not been declared.
function square(3) {
return num * num;
}

// =============> write the error message here

// =============> explain this error message here
// =============> SyntaxError: Unexpected number

// =============> As I expected JavaScript has flagged the use of a number as the function parameter, as it needs to be a variable name.

// 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
// =============> Right now the first console.log inside the function prints 320 - but the function does not have a return statement, so when it is used inside the template literal it will show as undefined. The code should use return a * b instead of 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)}`);

// =============> write your explanation here
// =============> This code returned "The result of multiplying 10 and 32 is undefined" as predicted because there is no return statement within the function.

// 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, 21)}`)
9 changes: 6 additions & 3 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
// =============> As there is a ; after return this means this function will return 'undefined' as it will not be able to run a + b. There is no value after return. The console.log statement will be The sum of 10 and 32 is undefined.

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

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

// =============> write your explanation here
// =============> As predicted the console.log statement returned The sum of 10 and 32 is undefined. To fix this I will remove the ; after return so that the function can return the sum of a + b
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>
function sum(a, b) {
return a + b;
}
13 changes: 8 additions & 5 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here

// =============> Num has been declared as 103. The function converts the number to a string and then returns the last number of that string. The return statement specifies the last digit of num which is already declared as 103 - so no matter what number someone puts in the temperate literals within the console.log statements 3 is always going to be returned.
const num = 103;

function getLastDigit() {
Expand All @@ -14,11 +13,15 @@ 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 predicted, 3 is always going to be returned as the num was specified as 103 and the parameter within the function was not declared. To fix this const num = 103 needs to be removed and the variable num needs to be placed as a parameter within the function getLastDigit
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============>
function getLastDigit(num) {
return num.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
11 changes: 9 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,12 @@
// 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 heightSquared = height * height;
const bmi = weight / heightSquared;
const bmiMultiplyByTen = bmi * 10;
const bmiRounded = Math.round(bmiMultiplyByTen);
const finalBmi = bmiRounded / 10;
return finalBmi;
}

console.log(calculateBMI(58, 1.52));
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(str) {
str = str.toUpperCase();
str = str.replaceAll(" ", "_");
return str
}

console.log(toUpperSnakeCase("harry potter and the philosopher's stone"));
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)
.padEnd(2, "0");

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

console.log(toPounds("13200p"))

// removed the unchanging const variable of 399p so we can have various inputs
// added a function with the variable name as the parameter
// changed console.log to return so that it's more reusable
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 @@ -15,24 +15,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
// =============> Pad is called 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 is the value assigned to num as the first pad is totalhours which calculates to be 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> "00" - this is because it has been stated within the function that if the value's length is less than 2 which 0 is, then add another 0 at the beginning ("0" + numString;). Also toString converts 0 from a number to a string which is why I included ""

// 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 is remainingSeconds which is 1 so the value assigned to num is 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
// =============> "01" - same logic as quested c. Also as "01" is not shorter than length of 2 the loop closes and doesn't run again
Loading