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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
.vscode/
**/.DS_Store
12 changes: 9 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// I think this function capitalise is meant to make the first letter of a string into uppercase then return the string

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

// =============> write your explanation here
// =============> write your new code here
// I got the error "SyntaxError: Identifier 'str' has already been declared"
// to fix this, I just removed the word let in line 8. This gave a value to str without trying to declare it again
// I ran the new code and there were no errors

// function capitalise(str) {
// str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }
23 changes: 20 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> decimalNumber is defined within the function so it will not be recognised in console.log(decimalNumber);

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

Expand All @@ -14,7 +14,24 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> this is a function to convert a decimal number to a percentage - function declared
// the decimal number is declared as 0.5
// percentage is calculated by multiplying the decimal // number by 100 and then adding the % symbol
// the answer returned is named percentage and gives a // number followed by %
// the answer will be printed in console

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> // the error was actually 'decimalNumber' has already been declared
// this was because decimalNumber was declared inside the function convertToPercentage(decimalNumber)
// and declared again within the function with the const statement
// once const decimalNumber = 0.5; was moved outside the function this error was resolved
// that way the global decimalNumber exists and the function can still accept it as an argument


// const decimalNumber = 0.5;
// function convertToPercentage(decimalNumber) {
// const percentage = `${decimalNumber * 100}%`;
// return percentage;
// }
// console.log(decimalNumber);

23 changes: 19 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@

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

// =============> write your prediction of the error here
// =============> I think that return can only return a value not do a calculation
// so this will return an error because it is trying to do a calculation instead of returning a value

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

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

// =============> explain this error message here
function square(3) {
return num * num;
}

// =============> SyntaxError: Unexpected number

// =============> this is because variables and parameters in functions cannot start with a number or be a number
// however, return can be used for calculations so my prediction was wrong


// Finally, correct the code to fix the problem

// =============> write your new code here
// =============>
// function square (num) {
// return num * num;
// }
// console.log(square(5));

// ran console.log to ensure it works



23 changes: 20 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
// Predict and explain first...

// =============> write your prediction here
// =============> when the console.log outside the function is performed $(multiply(10, 32))
// it will print the text The result of multiplying by 10 and 32 is
// but then will state undefined for the value


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
// =============> there is no return
// console.log(a*B) printed the result of multiplying a and b but because there is no return
// the answer was not returned to the function so the console.log outside the function did not
// receive the value and printed undefined instead


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

// I removed the first console.log because it didn't seem necessary to print out 320
// if you need to print 320 as well as the final console-log statement then console.log(a * b)
// can be added back in but it would need to be before the return statement because if it is
// after the return statement it will never be reached and the value will not be printed out

16 changes: 13 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// =============> the function will not perform a + b and will return undefined in the
console.log function


function sum(a, b) {
return;
Expand All @@ -8,6 +10,14 @@ function sum(a, b) {

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

// =============> write your explanation here
// =============> there shouldn't be a semi-colon after return
// this will signify the end of function so a + b will not be performed therefore there
// is no answer to return to print in the console.log function

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

38 changes: 34 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> the function has no parameter so will use the
// global value of num for num.toString which means it will always
// return 3 which is the last digit of 103


const num = 103;

Expand All @@ -14,11 +17,38 @@ 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 has no parameter so will use the
// global value of num for num.toString

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

// removed const num = 103; and then added num as a parameter to the function
// getLastDigit so that it can take in the value of num when the function is
// called and return the last digit of that number instead of always using
// the global value of num which was 103.

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

// num was not declared within the function so the global value of num in the
// the const declaraton was used for each number
//I corrected this by putting num as a parmaeter for the function so that it can take
//in the value of num when the function is run for any number and return the last digit
//of the number instead of always using the fixed value of num which was 103.
//I also removed the const declaration of num because it was not needed and was causing
//confusion as to which value of num was being used in the function.

8 changes: 7 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// this will be a number to 1 decimal place
const bmi = weight / (height * height); //calculation to calculate BMI
return Number(bmi.toFixed(1));
}
console.log(calculateBMI(70, 1.73));

// tested and it works, the function calculates the BMI correctly and returns it to 1 decimal place.
17 changes: 17 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,20 @@
// 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 str.toUpperCase().split('').map(function(c) {
// splits string into individual characters and maps each character to a new value

return /[A-Z0-9]/.test(c) ? c : '_';
// checks if the character is an uppercase letter or a digit

}).join('');
// joins the array of characters back into a single string and replaces spaces or special
// characters such as "?" with underscores
}

console.log(toUpperSnakeCase("there-once was/a young lady from+London"));
console.log(toUpperSnakeCase("hello.world! test+123"));

18 changes: 18 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,21 @@
// 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 getPenceString(penceString){

const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1);
//removes p
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//puts zeros in front of number if less than 3 digits
const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);
//removes last 2 digits to get pounds
const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
//gets last 2 digits to get pence, if less than 2 digits adds zeros to end of string but shouldnt be less than 3
//due to padStart above
return `£${pounds}.${pence}`;
// returns string with pounds and pence in correct format
}

console.log(getPenceString("399p"));
// tested with 123p 1200p 24589p 89p 9p and 0p
Comment on lines +8 to +24
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is off.

Have you installed prettier VSCode extension and enabled formatting on save/paste on VSCode
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?

Copy link
Author

Choose a reason for hiding this comment

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

I did install prettier and enabled formatting on save/paste. I may have done it after I had done this or it may not be working.

Copy link
Contributor

Choose a reason for hiding this comment

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

Understood.

I cannot keep track of whom I had reminded of this. So I just nagged the person with this reminder whenever I spotted improperly formatted code.

28 changes: 23 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,43 @@ 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 3 times, once for pad(totalHours,
// once for (pad)remainingMinutes and once for (pad)remainingSeconds


// 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 first call to pad is pad(totalHours).
//totalHours is calculated as totalMinutes // 60.
//For the input 61, seconds is 61, remainingSeconds is 1, and totalMinutes is 1.
//So, totalHours is 1 // 60 = 0.
//Therefore, num is assigned the value 0 when pad is called for the first time.


// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> The first call to pad is pad(totalHours).
//totalHours is calculated as totalMinutes // 60.
//For the input 61, totalMinutes is (61 - 1) // 60 = 1.
//So, totalHours is 1 // 60 = 0.
//pad(0) returns the string representation of 0, padded with zeros to a minimum length of 2, which 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
// =============> We look at the last call to pad, which is pad(remaining_seconds).
//remaining_seconds is calculated as seconds % 60.
//For the input 61, seconds % 60 equals 1.
//So, pad(remaining_seconds) is equivalent to pad(1). The value assigned to num in the last call to pad 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
// =============> For the input 61, remaining_seconds will be 1. So, pad(1) will return "01".
// Therefore, the value assigned to num is 1.
2 changes: 2 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

console.log formatAs12HourClock("14:00"); // should return "2:00 pm"