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
17 changes: 11 additions & 6 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: The function is capitalise is supposed to the take the first letter (at index 0) of the word, then turn it to a capital letter. And the slice (1) is supposed to delete the first letter at index 0, of a word and write the remaining letters. Then combine the first capital letter with the rest of the word which stayed the same except for the first letter which was removed with the slice method.

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

// =============> write your explanation here
// =============> write your explanation here: There is an error in line 8 because we are using 'str' to declare a new variable meanwhile this name has previously been used. WE should get a new name.
// =============> write your new code here
function capitalise(str) {
let strOne = `${str[0].toUpperCase()}${str.slice(1)}`;
return console.log(strOne);
}
capitalise("manhood");
27 changes: 18 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...

// The function is trying to convert a decimal number into a percentage.
// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> write your prediction here: in the function, on line 9, the variable name 'decimalNumber' has already been used on line 8, so we need a new name or else, we will get a referencing error. ON line 15, the 'decimalNumber' cannot be printed because it is a local variable which exists only inside the function 'convertToPercentage' and cannot be access when outside of this function.

// 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
// =============> write your explanation here: When running the code, I get an error on line 9 for because I used the variable name decimalNumber a 2nd time. Then there is a reference error on line 15 because 'decimalNumber' cannot be accessed when outside of the function. Also, we do not really need the code on line 9.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const firstDecimal = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.1));
console.log(convertToPercentage(0.5));
20 changes: 12 additions & 8 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@

// Predict and explain first BEFORE you run any code...

// THere will be an error on line 8 because we used a value (number 3) instead of a parameter. Functions are designed to be reusable.
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> write your prediction of the error here: We will get an error because we used a value (3) instead of a parameter.

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

// =============> write the error message here
// =============> write the error message here: The error says that "unexpected number ". And it comes from line 8.

// =============> explain this error message here
// =============> explain this error message here: We are suppose to use a parameter instead of a hard value.

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

17 changes: 11 additions & 6 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
// =============> write your prediction here: The function console.log on line 6 doesn't return any value. It's only function is to print out.

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
// =============> write your explanation here: Because we didn't have a return statement for our function 'multiply', we got an undefined value.

// 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: 13 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here: Line 5 and line 6 are not connected so as they are separated by ';', there is nothing that the function is returning.

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
// =============> write your explanation here: We have an undefined value for the sum of a and b because we are not returning anything.
// 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)}`);
28 changes: 19 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Our function doesn't take a parameter. So, it is not reusable and will always consider num = 103 . The num.toString() method converts everything into a String. Then the slice(-1) extracts the last figure of the num and give it back to us.

// const num = 103;

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

// Now run the code and compare the output to your prediction
// =============> The output is the same as my prediction
// Explain why the output is the way it is
// =============> The function isn't reusable because we didn't add a parameter to the function getLastDigit(). So, it will always take num = 103 because that is what line 9 does.
// Finally, correct the code to fix the problem
// =============> write your new code here
const num = 103;

function getLastDigit() {
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)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Because our function doesn't take a parameter, line 9 is actually calling the global variable which is const num = 103. That is why all out return output will be 3. That is why I allocated a parameter to our function, though I gave it the name 'num'.
8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const heightSquare = height * height;
const newBMI = weight / heightSquare;
const toOneDecimal = Math.round(newBMI * 10)/10;
return toOneDecimal
}

console.log(calculateBMI(70, 1.73));
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 upperCaseGroup(sentence){
const newSentence = sentence.toUpperCase();
return newSentence.replaceAll(" ","_");
}

console.log(upperCaseGroup("hello there"));
console.log(upperCaseGroup("lord of the rings"));
14 changes: 14 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,17 @@
// 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}`;
return console.log(`£${pounds}.${pence}`);

}
// console.log(toPounds("399p"));
// console.log(toPounds("500p"));
toPounds("399p");
toPounds("4596p");
10 changes: 5 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad is called zero time.

// 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
// =============> when pad is called the first time, num is 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> The return value of pad is "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
// =============> The value of num when pad is called the last time is 1. This is because the last call to pad() is pad(remainingSeconds) and formatTimeDisplay(61) calculates remainingSeconds as 1 (61 % 60 = 1), the value passed into pad() as 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
// =============> pad returns "01" when it is called the last time. This is because the function pad add a "0" in front of num as long as it is lesser than 2. And it comes from pad(remainingSeconds) which is (61 %60 = 1);
Loading