Skip to content
12 changes: 11 additions & 1 deletion Sprint-3/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Predict and explain first...
// =============> write your prediction here
// =============>The function will return capital S because the first letter of the str variable was capitalised with the toUpperCase method

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// The str identifier has already been declared in the function parameter and cannot be declared anywhere else inside the function as a variable

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


// =============> write your explanation here
// When a parameter is declared in a function, it causes an Error if it is declared inside the function as a variable, either you change the name used for the instructions or remove let
// =============> write your new code here

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

console.log(capitalise("hello"));
12 changes: 10 additions & 2 deletions Sprint-3/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
// because the decimanlNumber variable has already been declared and you cant declare it twice, also the parameter needs to not be tied to any value so it works as intended

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

Expand All @@ -15,6 +15,14 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// decimalNumber creates a variable in the function scope, then const decimalNumber tries to create a variable with the same name in that same scope, then it hits a Syntax error. Then the program stops running

// 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));
12 changes: 7 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,20 @@

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

// =============> write your prediction of the error here
// =============> Because the parameter needs to possess a name, i believe this will throw an error

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

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

// =============> explain this error message here
// =============> Because a parameter is a placeholder name for the function, it should receive a value when the function is called, it cant be a number.

// Finally, correct the code to fix the problem

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

function square(num) {
return num * num;
}
console.log(square(3));

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

// =============> write your prediction here
// =============> The function instructions will work but because there is no return statement it will be 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
// =============> Because a return statement will return that value back to the code, it will send it back to who called the function this does not mean the human will see it in the log, console.log is used to show the result to the reader.

// 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)}`);
13 changes: 10 additions & 3 deletions Sprint-3/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
// =============> This will not work because the a + b statement should have been after return, but return has a semicolon after it which stops the function an returns nothing

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

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

// =============> write your explanation here
// =============> Because return and a+b are not on the same line, the line after return is never reached, and would be considered dead code and therefore the console logs argument as 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)}`);
24 changes: 21 additions & 3 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
// =============> This function aims to return the number as a string using the toString method, and returns the last index in the string using the slice method with -1. the issue is that the number variable was declared outside the function as const, so the last digit will always be 3

const num = 103;

Expand All @@ -15,10 +15,28 @@ 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 num variable was declared outside the function which returns num with the string methods accessed with it. The function also has no parameter to receive the value passed in the function.

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

// The reason it was not working properly is because there was no parameter that can receive the value passed in the function, and so when the function is called it defaults to the num const variable in the global scope
3 changes: 3 additions & 0 deletions Sprint-3/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
// It should return a string of their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const bmi = weight / (height**2);
return bmi.toFixed(1)
// return the BMI of someone based off their weight and height
}
console.log(`The BMI of Jacob based on his weight and height is ${calculateBMI(75,1.70 )}`);
8 changes: 8 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,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(cap) {

return cap.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
25 changes: 25 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,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("790p"));
console.log(toPounds("70p"));
console.log(toPounds("8950p"));
console.log(toPounds("6p"));
10 changes: 5 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,18 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> It will be called 3 times in template lateral

// 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 because template laterals run from left to right and pad(totalHours) is 0

// c) What is the return value of pad when it is called for the first time?
// =============> write your answer here
// =============> it returns "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 last pad called is pad(remainingSeconds) which is 61 % 60 so 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
// =============> it returns "01" because numString is "1" with length of 1 which is less than 2, the loop adds 0 to the front so its 01
Loading