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
14 changes: 13 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// I predict that the code will throw an error due to the fact that the function redeclares the
// the str using let str. This is not allowed as it is already defined as a parameter.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +11,15 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
//console.log(capitalise("hello"));
// =============> write your explanation here
// The error occures due to the fact that str is already declared as a function parameter.
// Using let str inside the function tries to redeclare the same variable, which is causing a syntaxError for Javascript.

// =============> write your new code here
// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}

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

// Why will an error occur when this program runs?
// =============> write your prediction here
// My prediction is that this will solution will fail due to the fact that the decimalNumber is being declared twice.
// The second issue is the decimalNumber is being logged to the console, but this is parameter for convertToPercentage.
// So to call the function accurately we need to call convertToPercentage not decimalNumber.

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

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

// =============> write your explanation here
// The function convertToPercentage takes a decimal number as parameter.
// It multiplies the number by 100 and converts it into a string with a "%" symbol.
// The corrected version removes the duplicate declaration and calls the function properly.

// 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));
17 changes: 17 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,34 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// This function will cause a syntax error due to the fact that the parameter should be variable name, not a literal value.
// The second issue is that "num" is used inside the function however it is not defined anywhere.

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

// =============> write the error message here
// SyntaxError: Unexpected number
// at internalCompileFunction (node:internal/vm:76:18)
// at wrapSafe (node:internal/modules/cjs/loader:1283:20)
// at Module._compile (node:internal/modules/cjs/loader:1328:27)
// at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
// at Module.load (node:internal/modules/cjs/loader:1203:32)
// at Module._load (node:internal/modules/cjs/loader:1019:12)
// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
// at node:internal/main/run_main_module:28:49

// =============> explain this error message here
// This error message means that JS expects a parameter name within the function.
// The second error message is "num" is not declared.

// 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: 9 additions & 1 deletion Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// Predict and explain first...

// =============> write your prediction here
// The function multiply(a, b) logs the result within the function but does not return anything.

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
// Console.log() will print something.
// Return gives a value back to the caller - the function currently will print the result but does not return it.

// 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)}`);
31 changes: 21 additions & 10 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
// Predict and explain first...
// =============> write your prediction here
// Predict and explain first...
// =============> write your prediction here
// I predict that the function will not return undefined.
// This is due to the fact that the return statement is on its own line, therefore trying to do the addition will not work as the code is not read.

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
// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> write your explanation here
// The return statement is written on its own line, therefore JS automatically inserts a semicolon after the return.
// This means that the function exits immediately and returns undefined.
// The expression a + b; is not executed.

// 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-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@

// Predict the output of the following code:
// =============> Write your prediction here
// I predict that the three console.log messages will always output 3.
// This is due to the fact that the const num is defined as 103 and the program will always use that global value as the source of truth.

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)}`);
//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
// 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
// The global value that is defined outside of the function is used as the source of truth.
// Therefore when the program executes the line return num.toString().slice(-1); it always considers the global value.
// The console.log ignores the values.

// 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
10 changes: 8 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

// solution 1
function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height ** 2);
return Number(bmi.toFixed(1));
}
console.log(`My BMI is: ${calculateBMI(55, 1.3)}`)

// solution 2
// To make this program fit for real world use, I would implement a conversion for various metrics used for height and weight.
5 changes: 5 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,8 @@
// 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
// Solution
function toUpperSnakeCase(str) {
return str.toUpperCase().replaceAll(" ","_");
}
console.log(ToUpperSnakeCase("hello there"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost! Did you try running this code to see what happens?

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

//Solution:
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("399p"));
console.log(toPounds("45p"));
console.log(toPounds("5p"));
console.log(toPounds("1200p"));
10 changes: 10 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,34 @@ 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 will be called three times (for hours, minutes and seconds)

// 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 value assigned to num for the first time that it is called is 0 (totalHours = 0)

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// The return value of pad for its first call 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 is 1, when formatTimeDisplay(61) runs: 61 seconds = 1 minute and 1 second
// remainingSeconds = 61 % 60 = 1
// The last call to pad is pad(remainingSeconds), so num receives 1

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// The return value is "01" - pad converts the number to a string and ensures it has at least 2 digits using padStart(2, "0")
// Since "1" has only one digit, a leading zero is added so the value is then "01"