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
23 changes: 19 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@
// 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) {
// const capiLetter = `${str[0].toUpperCase()}${str.slice(1)}`;
// return capiLetter;
// }

// =============> write your explanation here

// The original code failed for two reasons: first the function was never invoke,
// there was a variable naming conflict with the function scope.
// Since str was already defined as a parameter, can not be used to re-declare
// a variable inside the function. To fix this, I moved the processed directly and
// returned the result directly

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


function capitalise(str) {

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

console.log(capitalise("hello"));
33 changes: 27 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
// Predict and explain first...

const { t } = require("tar");

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

// This function will throw a SyntaxError because decimalNumber
// is being redeclared inside the same scope as the parameter.
// Additionally, the function ignores the input parameter
// because the value is hardcoded inside the body.
// Finally, the console.log will throw a ReferenceError
// because it tries to access a local variable from the global scope.
// To fix this, we must remove the internal declaration to use the
// parameter dynamically and invoke the function correctly.

// Finally, correct the code to fix the problem
// =============> write your new code here



function convertToPercentage(num) {

const percentage = `${num * 100}%`;
return percentage;
}

console.log(convertToPercentage(0.80));
20 changes: 17 additions & 3 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@

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

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


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

// It will throw a syntaxError because a literal Number(3) cannot be used
// as a function parameter. furthermore, the return statement will
// cause an referenceError because it attempts to use a var num that
// has no been defined. To fix this, we must replace the number in
// the function with the identifier num.

// =============> explain this error message here

// Finally, correct the code to fix the problem

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



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


console.log(square(9));
17 changes: 13 additions & 4 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

// =============> write your prediction here

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

// =============> write your explanation here

//will display the first console.log and make the operation 10 * 32
// but the second console.log will throw undefined because
// the function does not have a return statement.

// 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)}`);
21 changes: 16 additions & 5 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

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

// This function will return undefined because the return statement executes first.
// since the operation a + b is after the return statement, it will never be 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 + 32 is ${sum(10, 32)}`);
43 changes: 35 additions & 8 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,49 @@
// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
// The output will always be 3 for all cases. Even though
// arguments are passed to the function, it lacks parameter
// to receive them. Therefore, the function defaults to using
// the global scope constant num = 103 every time it runs.

function getLastDigit() {
return num.toString().slice(-1);
}
// const num = 103;

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)}`);
// 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
// =============> write the output here
// The output is:
// 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 function getLastDigit is not working properly because
// it is hardcoded to use the global constant num. Since
// the function definition does not include any parameters,
// it ignores the arguments (42, 105, 806) passed during the
// call. As a result, it always calculates the last digit of 103, which is 3

// Finally, correct the code to fix the problem
// =============> write your new code here

const num = 103; //this now is a ghost variable and is not used in the function

function getLastDigit(value) {
return value.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
// Explain why getLastDigit is not working properly - correct the problem
18 changes: 16 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,19 @@
// 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
}
// return the BMI of someone based off their weight and height
const squareHeight = height * height;
const operation = weight / squareHeight;
console.log(operation);

const bmi = Math.round(operation * 10) / 10;

return bmi;
}

console.log(calculateBMI(70, 1.73));


// to get 23.4 we use the Math.round(operation * 10) / 10; to move the decimal
// one place to the right, round it to the nearest whole number, then move it
// back by dividing by 10.
11 changes: 10 additions & 1 deletion Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
// Another example: "lord of the rings" should be "LORD_OF_THE_RINGS"

// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// Use the MDN string documentation to help you find a solutio
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase


function toUpperSnakeCase(str) {

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

// console.log(toUpperSnakeCase("Hello there")); // "HELLO_THERE"
console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS"
75 changes: 75 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,78 @@
// 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


// =============================== Original Code ========================
// const penceString = "399p";

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

// console.log(`£${pounds}.${pence}`);



// =============================== Refactoring ========================

// to make robust function we can use some maths methods to reduce the looping and
// return only the numbers, like this: /[^\d]/g, with that we can remove all
// characters that are no number, then we can convert the number into decimals by
// dividing by 100.

//padStart method can be used to ensure that strings have a minimum length, no used in numbers
//The toFixed() method formats a number using fixed-point notation.

// In programming, we call this a Regular Expression (Regex). Think of it as a custom filter for text.

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions

// REGEX BREAKDOWN

// Delimiters (/ /): The "brackets" that mark the start and end of the Regex.
// Non-digit (\D): Matches any character that is NOT a number (0-9).
// Negated Set ([^\d]): The long version of \D (Anything that is NOT a digit).
// Global Flag (g): Tells the code to clean the entire string, not just the first letter.
// Empty String (''): Replaces the "bad" characters with nothing (deleting them).
// Example: "£1.50p" -> "150"
// const onlyNumbers = str.replace(/\D/g, '');

// =============================== Reusable Code ========================


function toPounds(str){

const onlyNumbers = str.replace(/[^\d]/g, '');
const decimals = (onlyNumbers/100).toFixed(2);

return `£${decimals}`;
};



// =============================== 1st way to test ========================

const examples = ["79p", "hfhbgjk1p", "150p", "£2.50", "11000"];
for (const example of examples) {
console.log(toPounds(example));
}


// =============================== 2nd way to test ========================

// const examples2 = ["79p", "hfhbgjk1p", "150p", "£2.50", "12000"];
// for (const example of examples2) {
// console.log(`Original: ${example} -> Formatted: ${toPounds(example)}`);
// };
Loading