diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..7add48f11 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here - +//This code will throw an error as we haven't passed an argument into str? or str is the function parameter and variable already on line 5 so it may throw an error to declare again as a variable 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; +//} +//console.log(capitalise("mars")); +//SyntaxError: Identifier 'str' has already been declared +// This code threw an error as "str" has already been defined in the function capitalise(str) so let str is causing the error as it cannot be defined twice. function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + let result = `${str[0].toUpperCase()}${str.slice(1)}`; + return result; } - -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("mars")); +// In my new code i'm calling the function capitalise(str) in my code "str" is only the parameter and not the variable aswell. In the variable result it will take any argument and using the toUpperCase method itll make a copy of the the letter at index zero as a capital letter so as i passed in "mars" itll return M and store it in the variable result. The method slice will return a copy of the argument from index 1 onwards so "ars" and because it's in a template literal it will join "M" and "ars" together and this is what will get stored in result and by returning result on line 14 i'm asking the function to return what is stored in result so javascript can print it using console.log or i can use it elsewhere as i'm coding. diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..c8b3dacda 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,24 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// like the original code in 0.js a syntax error will occur because decimalNumber has already been declared as the parameter in the function so it cannot be redeclared inside the function with the same name. // Try playing computer with the example to work out what is going on +//function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; + +//return percentage; +//} + +//console.log(decimalNumber); +//Because decimalNumber is declared twice it wont work but when i call the function convertToPercentage and pass in 0.5 it will get assigned to the parameter decimalNumber so i need to update the code to reflect this. + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; - return percentage; } +console.log(convertToPercentage(0.5)); -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +//console output: 50% diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..9fe1fd957 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,23 @@ - // 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 +// I think this code will throw an error because we are passing an actual number into the parameters but javascript only allows a variable to be passed into the parameters that can be used to store information so i would need to change the number 3 into num ( and num here would act as the placeholder for any number passed in when the function gets called later) for this code to work because as it is i believe num would be undefined as it hasn't been defined anywhere. -function square(3) { - return num * num; -} +//function square(3) { +// return num * num; +//} -// =============> write the error message here +// error message: SyntaxError: Unexpected number -// =============> explain this error message here +// This error means javascript couldn't run the code because it doesn't accept numbers as a parameter. // Finally, correct the code to fix the problem -// =============> write your new code here +function square(num) { + let numResult = num * num; + return numResult; +} +console.log(square(3)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..f56308e54 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// here we are declaring a function called multiply which takes in two arguments and multiplies them but because the function doesn't return the value when we call the function it hasnt been stored anywhere for it to be called upon so i think it will return undefined in the template literal. I also think because there are two console logs one will print the value 320 and the other will print the template literal but as multiply hasn't been stored anywhere it will print undefined ? -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 +// the function as it is only storing the result of a and b in two console logs one asking for the total which is 320 so it prints it and the other is template literal so it prints the template literal but multiply is undefined. // Finally, correct the code to fix the problem -// =============> write your new code here +function multiply(a, b) { + let multiplyResult = a * b; + return multiplyResult; +} + +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..7ae834194 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// The function will not return the expected result because whilst sum takes in two arguments it cannot add a+b together because it hasn't been stored in a variable before return so when we call sum it will be undefined? + +//function sum(a, b) { +// return; +// a + b; +//} + +//console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +//console output: The sum of 10 and 32 is undefined +// sum is undefined because we haven't stored the operation we want it to perform correctly in a variable so its value is undefined. +// Finally, correct the code to fix the problem function sum(a, b) { - return; - a + b; + let sumResult = a + b; + return sumResult; } - console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..1e67cc94c 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,24 +1,31 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// here we have a constant variable called num which stores the number 103 we then have declared the function getLastDigit which has an empty parameter we are then asking it to return the value stored inside num and apply the .toString method to it which turns the number 103 into a string "103" we are then taking "103" and applying the .slice method to it which will start from position -1 and return everything after it which leaves us with the string "3" then outside of the function, the function getLastDigit is being called 3 times in template literal using console.log with the arguments 42, 105 and 806 but because the variable num = "3" it will print the template literal with the "3" in the console when javascript gets to the function call for each value, which isn't the expected result. -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)}`); +//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 +// output: 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 console output prints this because the the methods have only been applied to the number stored in the num variable so javascript isn't able to access the numbers passed in when the function is called in the template literal as the function getLastDigit() has no parameter so it passes in the value stored in the num variable which is "3" and so every time the function is called "3" is returned. // Finally, correct the code to fix the problem -// =============> write your new code here +function getLastDigit(num) { + let getLastDigitResult = num.toString().slice(-1); + return getLastDigitResult; +} +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 diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..e41cebf9c 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,11 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + // Store height squared (calculation wont change so can be stored in const) + const heightSquared = height * height; + // Store weight divided by height squared (calculation wont change so can be stored in const) + const bmi = weight / heightSquared; + //return bmi to 1 decimal place + return bmi.toFixed(1); } +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..1a4b9b4f8 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 + +// define function +function stringToUpperCase(str) { + //store string in upper snake case (calculation wont change store in const) + const stringToUpperCaseResult = str.toUpperCase().replaceAll(" ", "_"); + // return result + return stringToUpperCaseResult; +} +// call function +console.log(stringToUpperCase("welcome to cyf")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..c6e31f92e 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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(pence) { + //Declare penceString variable + let penceString = pence; + // Store penceString value with pence on the end and make sure its at least 3 characters long add 0 at start if needed. + let penceStringWithoutTrailingP = penceString + .substring(0, penceString.length - 1) + .padStart(3, "0"); + // copy everything apart from the last two characters + let poundsResult = penceStringWithoutTrailingP.substring( + 0, + penceStringWithoutTrailingP.length - 2, + ); + // start at the last two characters and take everything from there to end and make sure that value is two characters long add 0 if needed + let penceResult = penceStringWithoutTrailingP + .substring(penceStringWithoutTrailingP.length - 2) + .padEnd(2, "0"); + // return result of poundResult and penceResult in a template string + return `£${poundsResult}.${penceResult}`; +} +//call function to see if works with different inputs +console.log(toPounds("399p")); +console.log(toPounds("1000p")); +console.log(toPounds("15000p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..7337cfb25 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -15,24 +15,26 @@ 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 which is seen on line 15 // 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 +// 0 // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// the return value of pad when called for the first time 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 +// 1 second is stored in remainingSeconds so the value 1 is assigned to num when pad is called for the last time. // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// the return value is 01 as the variable numString stores "01" diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..c7de2b427 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -2,24 +2,89 @@ // Make sure to do the prep before you do the coursework // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. +//function formatAs12HourClock(time) { +// const hours = Number(time.slice(0, 2)); +// if (hours > 12) { +// return `${hours - 12}:00 pm`; +// } +//return `${time} am`; +//} + function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; + } else if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } else if (hours === 12) { + return `12:${minutes} pm`; + } else { + return `${time} am`; } - return `${time} am`; } +//current = what the code did +//target = what i wanted it to do + const currentOutput = formatAs12HourClock("08:00"); const targetOutput = "08:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); const currentOutput2 = formatAs12HourClock("23:00"); const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, +); + +const currentOutput3 = formatAs12HourClock("12:00"); +const targetOutput3 = "12:00 pm"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, +); +const currentOutput4 = formatAs12HourClock("23:59"); +const targetOutput4 = "11:59 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, +); + +const currentOutput5 = formatAs12HourClock("00:00"); +const targetOutput5 = "12:00 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, +); + +const currentOutput6 = formatAs12HourClock("00:30"); +const targetOutput6 = "12:30 am"; +console.assert( + currentOutput6 === targetOutput6, + `current output: ${currentOutput6}, target output: ${targetOutput6}`, +); + +const currentOutput7 = formatAs12HourClock("14:30"); +const targetOutput7 = "2:30 pm"; +console.assert( + currentOutput7 === targetOutput7, + `current output: ${currentOutput7}, target output: ${targetOutput7}`, +); + +const currentOutput8 = formatAs12HourClock("15:30"); +const targetOutput8 = "3:30 pm"; +console.assert( + currentOutput8 === targetOutput8, + `current output: ${currentOutput8}, target output: ${targetOutput8}`, ); +console.log(formatAs12HourClock("14:30")); +console.log(formatAs12HourClock("15:30")); +//Edge cases noticed: +// minutes are ignored in the formatAs12HourClock function after 12 so all pm hours are printed incorrectly because minutes hasn't been stored anywhere in the function. +// 12 should return 12:00pm but it returns 12:00am in the function +// 00:00am should return 12:00am