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
35 changes: 30 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
// Predict and explain first...
// =============> write your prediction here
// I predict when this code runs, there will be a SyntaxError before the function executes.
// This is because the identifier 'str' has already been declared.

// call the function capitalise with a string input
// capitalise("hello");


// interpret the error message and figure out why an error is occurring
// Uncaught SyntaxError: Unexpected identifier 'string' - this is the error message.
// This error is occuring because the variable 'str' is being redeclared within the
// function, which is not allowed in JavaScript.
// The duplicate use if 'str' is causing the syntax error.


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

// The issue is variable redeclaration.
// str is the function parameter.
// let str tries to create a new variable with the same name.
// JavaScript does not allow redeclaring a variable in the same scope.
// As a result, the code throws a SyntaxError when it encounters the second 'str' declaration.

// New code without the error:

// 0.js — fully fixed version
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
if (!str) return str; // handles empty string
return str[0].toUpperCase() + str.slice(1);
}

// =============> write your explanation here
// =============> write your new code here
// Test it
console.log(capitalise("hello")); // should print "Hello"
console.log(capitalise("world")); // should print "World"
console.log(capitalise("")); // should print ""
32 changes: 22 additions & 10 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict an error will occur when this program runs because the variable 'decimalNumber' is already declared inside the function 'convertToPercentage' and cannot be redeclared.

// Try playing computer with the example to work out what is going on
// When the 'function convertToPercentage(decimalNumber)' is created,
// Javascript already creates a variable called decimalNumber. then, inside the function,
// 'const decimalNumber = 0.5' tries to create another variable with the same name. This
// casues a syntax error because you cannot declare two variables with the same name in the same scope.

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
// I redelcared the parameter decimalNumber using const.
// I tried to log decimalNumber outside its scope.
// The variables declared inside the function are not available outside unless they are returned.

// 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)); // "50%"
30 changes: 19 additions & 11 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@

// Predict and explain first BEFORE you run any code...
// This function should square any number but instead we're going to get an error

// 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:
// I predict that the error will be a syntax error because the parameter '3' is not a valid variable name.
// When you define a function,, the thing inside the parentahese must be a parameter name, NOT a number.

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

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

// =============> explain this error message here
// Explain the error messge here:
// When defining a function, inside the parentheses, you need to put a parameter name, which is a variable.
// In this case, '3' is not a valid parameter name because it is a number, not a variable.
// This causes a syntax error because Javascript expects a name, not a number.

// Finally, correct the code to fix the problem
// Write your new code here

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

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

console.log(square(3)); // 9
console.log(square(5)); // 25
26 changes: 18 additions & 8 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
// Predict and explain first...
// Predict and explain first...write your prediction here
// I predict, when the code runs, it will print 320, but it will also print "The result of multiplying 10 and 32 is undefined".

// =============> write your prediction here
//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
// The function multiply(a, b) logs the result of a * b to the console but does not return a value.
// When multiply(10, 32) is called within the template literal, it logs 320 to the console but returns undefined.
// Therefore, inside the template literal, the value is undefined, which is why the final output
// becomes "The result of multiplying 10 and 32 is undefined".

// Finally, correct the code to fix the problem
// Write your new code here
// To fix the problem, the function should return the result instead of logging it.

function multiply(a, b) {
console.log(a * b);
return a * b;
}

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

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

// Finally, correct the code to fix the problem
// =============> write your new code here
28 changes: 20 additions & 8 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Predict and explain first...
// =============> write your prediction here
// Predict and explain first...write your prediction here
// I think the code will return undefined.

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
// When JavaScript sees the 'return' statement, it immediately stopd the function.
// It does NOT continue to the next line.
// So this part, 'a+b;' never runs. To return the sum of a and b, we need to put it
// on the same line as the return.
// Like the: 'return a + b;'

// =============> write your explanation here
// 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)}`);
41 changes: 31 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// Write your prediction here
// I think the output will be 'The last digit of 42 is 2', 'The last digit of 105 is 5', and 'The last digit of 806 is 6'.
// I think this will happen because the getLastDigit function is supposed to take a number, convert it to a string, and
// then return the last charecter of that string, which should be the last digit.

const num = 103;
//const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
//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 output is 3 for all numbers because the function getLastDigit is not taking any parameters. It always uses the
// global variable `num` which is set to 103. So it always returns the last digit of 103, which is 3.

// Finally, correct the code to fix the problem
// Write your new code here

function getLastDigit(number) {
return number.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
// The function did not accept a parameter. It was a fixed variable (num = 103), so it always returned the last digit of 103.
28 changes: 26 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,30 @@
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
//function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// }

// Notes:
// BMI = weight ÷ (height x height)
// eg. BMI = 70kg ÷ (1.73m x 1.73)
// BMI = 70kg ÷ 2.99
// BMI = 23.41
// BMI = 23.4 (to 1 decimal place)

// I need to:
// 1. Square the height
// 2. Divide weight by squared height
// 3. Round the result to 1 decimal place
// 4. Return the result

function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

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



//toFixed(1) tells JavaScript to round the number to 1 decimal place.
21 changes: 21 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,24 @@
// 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

// NOTES:
// "hello there" should become "HELLO_THERE"
// I need to turn all letters into CAPITALS and replace the space " " with underscore "-"

// From the MDN documentation:
// I can use the toUpperCase() to turn the tring into all CAPS.
// I can use the replaceAll() to replace all the spaces with underscores.

// Good function name:
// toUpperSnakeCase - this name is decriptive and indicates that the function will convert a string to upper snake case.

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
59 changes: 58 additions & 1 deletion Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
// In Sprint-1, there is a program written in interpret/to-pounds.js

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


// You will need to take this code and turn it into a reusable block of code.
// 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
// You should call this function a number of times to check it works for different inputs.
// For example:
// toPounds("399p") should return "£3.99"
// toPounds("45p") should return "£0.45"
// toPounds("9p") should return "£0.09"
// toPounds("1200p") should return "£12.00"

// The current code removes the "p", makes sur ethe number has at least 3 digits, splits pounds and
// pences and prints it in money format.
// Right now, it only works for "399p". I need it to work for "5p" and "1234p".
// I need to turn it into a function. eg. 'function toPounds(penceString)'. penceString clearly
// decribes what the input is.

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")); // £3.99
console.log(toPounds("45p")); // £0.45
console.log(toPounds("9p")); // £0.09
console.log(toPounds("1200p")); // £12.00
Loading