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
30 changes: 26 additions & 4 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
// Predict and explain first...
// =============> write your prediction here

// I predict this will throw a SyntaxError because
// the variable 'str' is declared twice inside the function.



// 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) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }


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

// This code throws a SyntaxError: Identifier 'str' has already been declared.
// The function parameter is named 'str', and inside the function
// we try to declare another variable using 'let str'.
// JavaScript does not allow redeclaring a variable
// in the same scope.


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

console.log(capitaliseFixed("hello"));


35 changes: 29 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@
// Why will an error occur when this program runs?
// =============> write your prediction here

// I predict this will throw a SyntaxError because
// the parameter 'decimalNumber' is redeclared
// using const inside the function


// 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 code throws a SyntaxError: Identifier 'decimalNumber' has already been declared.
// The function already has a parameter called 'decimalNumber'
// Declaring 'const decimalNumber' inside the function
// redeclares the same variable in the same scope,
// which JavaScript does not allow.
//
// Also, console.log(decimalNumber) will cause a ReferenceError
// because decimalNumber is not defined outside the function.


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

28 changes: 25 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,38 @@

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

function square(3) {
return num * num;
}
// I predict this will throw a SyntaxError because
// a number (3) is used as a function parameter,
// which is not allowed in JavaScript.

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

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

// SyntaxError: Unexpected number

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

// This throws a SyntaxError: Unexpected number.
// Function parameters must be valid variable names.
// The number 3 is not a valid identifier, so JavaScript
// cannot parse the function definition.

// Also, the function uses 'num' inside the body,
// but 'num' is not defined anywhere.


// Finally, correct the code to fix the problem

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

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

console.log(square(3));



24 changes: 20 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,29 @@

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

function multiply(a, b) {
console.log(a * b);
}
// I predict the result will not be displayed correctly.
// The multiply function logs the result but does not return it,
// so the template literal will receive undefined.

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// 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 multiply function uses console.log to print the result,
// but it does not return the value
// When the function is used inside the template literal,
// it returns undefined because functions return undefined
// if there is no 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)}`);
24 changes: 19 additions & 5 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// Predict and explain first...
// =============> write your prediction here
// I think this might print undefined
// The return statement looks separate from a + b,
// so maybe the function is not actually returning the sum.

function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// function sum(a, b) {
// return;
// a + b;
// }
//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The function has 'return;' on its own line.
// When JavaScript sees return, it stops the function immediately.
// That means 'a + b' never runs.
// Because nothing is returned, the function returns 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)}`);

48 changes: 41 additions & 7 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,56 @@
// Predict the output of the following code:
// =============> Write your prediction here

const num = 103;
// I think the program will print 3 every time.
// Even though different numbers are passed into the function,
// the function is using the variable 'num', which is 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)}`);
// 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 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 is not using the number that is passed into it.
// Instead, it always uses the global variable 'num'
// which is set to 103.
// The last digit of 103 is 3
// so the function always returns 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)}`);


// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem

// The function was not working properly because it did not use
// the number that was passed into it.
// Instead, it used the global variable 'num',
// which was always 103.
// That is why it always returned 3.
//
// To fix the problem, I added a parameter to the function
// and used that parameter to calculate the last digit

7 changes: 5 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,9 @@
// 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) {const bmi = weight / (height * height);
return bmi.toFixed(1);
}
console.log(calculateBMI(70, 1.73));

// return the BMI of someone based off their weight and height
}
8 changes: 8 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,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(str) {
const upper = str.toUpperCase();
const result = upper.replaceAll(" ", "_");
return result;
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
25 changes: 25 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,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}`;
}

// test calls
console.log(toPounds("399p")); // £3.99
console.log(toPounds("50p")); // £0.50
console.log(toPounds("5p")); // £0.05
console.log(toPounds("100p")); // £1.00
19 changes: 19 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,35 @@ function formatTimeDisplay(seconds) {
// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here

// pad is called 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

// The value assigned to num the first time 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" because 0 becomes "0"
// and padStart adds a leading zero.


// 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 to num the last time is 1,
// because remainingSeconds is 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" because 1 becomes "1"
// when converted to a string, and padStart adds a leading zero.