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: 22 additions & 1 deletion Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...
// =============> write your prediction here
// =============> The code will throw an error because the variable str is a reserved keyword in JavaScript and cannot be used as a variable name.
// The error message will likely indicate that there is a syntax error or that the variable name is invalid.

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

console.log(str);

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

// C:\Users\alexo\CYF\Module-Structuring-and-Testing-Data\Module-Structuring-and-Testing-Data\Sprint-2\1-key-errors\0.js:8
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// ^

// SyntaxError: Identifier 'str' has already been declared

// The error message indicates that there is a syntax error in the code because the variable name 'str' has already been declared.

// In the function capitalise, the parameter 'str' is declared as a variable name, and then within the function body, there is another declaration of 'str' using the let keyword.
// This creates a conflict because 'str' is already declared as a parameter, and it cannot be redeclared as a variable within the same scope.

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

console.log(capitalise("alex"));
24 changes: 17 additions & 7 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> It will likely be a reference error because the variable decimalNumber is not defined in the global scope
// and is only declared within the function convertToPercentage. When we try to log decimalNumber outside of the function,
// it will not be accessible and will throw an error indicating that decimalNumber is not defined.

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

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

function convertToPercentage(a) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}
console.log(convertToPercentage(0.5));
19 changes: 12 additions & 7 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

// Predict and explain first BEFORE you run any code...

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

// =============> write your prediction of the error here
// =============> the code will throw a syntax error because the parameter name '3' is not a valid identifier in JavaScript.
// The error message will likely indicate that there is an unexpected number or that the parameter name is invalid.

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

// =============> write the error message here
// =============> C:\Users\alexo\CYF\Module-Structuring-and-Testing-Data\Module-Structuring-and-Testing-Data\Sprint-2\
// 1-key-errors\2.js:9 function square(3) {SyntaxError: Unexpected number

// =============> explain this error message here
// =============> We get an unexpected number error because the parameter name '3' is not a valid identifier in JavaScript.

// Finally, correct the code to fix the problem

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

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

console.log(square(4));
19 changes: 13 additions & 6 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

// =============> write your prediction here
// =============> The funtion does not return a value, so when we try to log the result of the function call,
// it will return undefined and log "The result of multiplying 10 and 32 is undefined" to the console.

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)}`);
//console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// =============> 320 The result of multiplying 10 and 32 is undefined

// 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)}`);
23 changes: 16 additions & 7 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Predict and explain first...
// =============> write your prediction here
// =============> the return statement is placed before the actual sum,
// which means that the function will return undefined before it can calculate the sum of a and b.
// Therefore, the output will be "The sum of 10 and 32 is 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
// =============> The program behaved as predicted
// Finally, correct the code to fix the problem
// =============> write your new code here

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
30 changes: 20 additions & 10 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> The const num variable is assigned in the global scope and returned in the getLastDigit function.
// The console logs lines 14-16 call the getLastDigit function due to this; the console will log "The last digit of 42 is 3",
// "The last digit of 105 is 3", and
// "The last digit of 806 is 3".

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

function getLastDigit() {
//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
// =============> The code performed as expected due to the declaration of num in the global and called 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)}`);

// 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
8 changes: 6 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,9 @@
// 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 bmi = weight / (height * height);
return Math.round(bmi * 10) / 10;
}

console.log(calculateBMI(85, 1.93));
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) {
// return the string in UPPER_SNAKE_CASE
return str.toUpperCase().replaceAll(" ", "_");
}

console.log(toUpperSnakeCase("hello there"));
console.log(toUpperSnakeCase("lord of the rings"));
23 changes: 23 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,26 @@
// 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("1399p"));
console.log(toPounds("360p"));
console.log(toPounds("55p"));
console.log(toPounds("5p"));
13 changes: 8 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ function formatTimeDisplay(seconds) {
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

console.log(formatTimeDisplay(10002));

// 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 3 times, once for each of the 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
// =============> num is assigned the value of 0, because 61 seconds is equal to 1 minute and 1 second, so the total hours 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 pad pads the number 0 to 2 digits with leading zeros.

// 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
// =============> num is assigned the value of 1, because 61 seconds is equal to 1 minute and 1 second,
// so the remaining seconds 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 pad pads the number 1 to 2 digits with leading zeros.