diff --git a/.gitignore b/.gitignore index bde36e5302..a4da420804 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules .DS_Store -.vscode -**/.DS_Store \ No newline at end of file +.vscode/ +**/.DS_Store diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..1d134af3dd 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -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 @@ -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; +// } diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..1c02b767a9 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -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 @@ -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); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..5922044d4b 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -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 + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..7dc01ccb95 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // 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); @@ -8,7 +11,21 @@ function multiply(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 + diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcfd..b4a4cbf474 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -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; @@ -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)}`); + diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..18863ec5f5 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -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; @@ -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. + diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..1ac2f387c0 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,10 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + // 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. diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..a9b0237c7e 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -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")); + diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..a08c01ca8d 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -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 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8c..683ed89ba6 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -10,6 +10,7 @@ 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 @@ -17,18 +18,35 @@ function formatTimeDisplay(seconds) { // 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. diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..2ac536f761 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,5 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +console.log formatAs12HourClock("14:00"); // should return "2:00 pm"