Skip to content
Open
19 changes: 16 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
// Predict and explain first...
// =============> write your prediction here
// =============> write your prediction here
// An error will be thrown because str has already been declared as the function's parameter.
// The function parameter and the let variable are both declared in the same function scope.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// 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
// The error occurs because str is already declared as the function's parameter, so it cannot be
// declared again using let inside the same scope. By changing the new variable's name to
// capitalisedStr, there is no longer a naming conflict and the code runs without any errors.

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

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

console.log(capitalise("mmm"));
15 changes: 12 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

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

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -14,7 +11,19 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

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

// =============> write your explanation here
// Since the parameter and variable have the same name, decimalNumber, we will get an error as we can not declare a
// variable of the same name with in the functions local scope

// 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));
13 changes: 9 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

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

// 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 have used a number instead of naming the parameter so we should get a syntax error

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

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

// =============> explain this error message here
// JavaScript read the number 3 as a number rather than the name of the parameter.
// Parameters must have valid names

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
9 changes: 9 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 function here is supposed to multiply both parameters by each other but the function multiply has no return so when the function is called by console.log it will return as undefined

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

// =============> write your explanation here
// The function here is supposed to multiply both parameters by each other but the function multiply has no return so when the function is called by console.log on line 10 it will return as undefined
// however this same function does say to output the result or a multiplied by b so the answer is outputted but not returned to line 10 and therefore we get undefined in that 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)}`);
9 changes: 9 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// I expect that we will get an undefined output on line 10 since the function does not return anything

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

// =============> write your explanation here
// The function sum defines that a+b must be added together but before that return is used with no argument defined and we we exit the function immediately when it is called on line 10 the function returns to the string undefined.

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

// Predict the output of the following code:
// =============> Write your prediction here
// Since line 7 uses a const variable that can not be changed I would expect the last digit to be remain three since num has been assigned to be 103

const num = 103;

Expand All @@ -14,11 +15,30 @@ 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
// =============> 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
// // Line 7 uses the const variable num which has been assigned the value 103 and the last digit of that number is 3. When the function getLastDigit is called it
// returns the last digit of num. Regardless of what number we use when calling the function it will always return 3 because the function has no parameter to read
// the arguments and always uses the value stored inside num instead.

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

// getLastDigit is not working properly because the function has no parameter to read the numbers used when it is called upon. Instead it always uses the const variable
// num which has been assigned the value 103 so it will always return the last digit of 3.
9 changes: 7 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,10 @@
// 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
}
let numWeight = Number(weight.slice(0, -2));
let numHeight = Number(height.slice(0, -1));
let BMI = numWeight / (numHeight * numHeight);
return Math.round(BMI * 10) / 10;
}

console.log(`Your BMI is: ${calculateBMI("70kg", "1.83m")}`);
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(string) {
return string.toUpperCase().replaceAll(" ", "_");
}

console.log(upperSnakeCase("i love cyf you guys are awesome!"));
22 changes: 22 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,25 @@
// 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 console.log(`£${pounds}.${pence}`);
}

penceToPounds("399p");
penceToPounds("580990p");
penceToPounds("489302849032p");
16 changes: 11 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,30 @@ 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
// The pad function is called upon 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
// num is = 0

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// the return value 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 assigned is 1 this is because when the value 61 is passed into the formatTimeDisplay function it sends totalHours,
// remainingMinutes and remainingSeconds in that order to the pad function. Since we know the remainingSeconds is sent last
// and that 61 divided by 60 leaves us with a remainder of 1 we know 1 will be the last value assigned to num.

// 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 return value will be "01" as when the value 1 is passed into the pad function it is converted into a string, which is "1" in this case,
// that string is then passed into a while loop which checks if the string is less than two characters and adds a "0" to the start of the string
// so "1" becomes "01" and is then returned.
Loading