diff --git a/Sprint-2/1-key-exercises/1-count.js b/Sprint-2/1-key-exercises/1-count.js index 117bcb2b6..fd85be739 100644 --- a/Sprint-2/1-key-exercises/1-count.js +++ b/Sprint-2/1-key-exercises/1-count.js @@ -1,6 +1,6 @@ let count = 0; count = count + 1; +console.log(count) -// Line 1 is a variable declaration, creating the count variable with an initial value of 0 -// Describe what line 3 is doing, in particular focus on what = is doing +// Line 3 has been able to reassign the value of the count variable from 0 to 1. The operator = assign the new value to be stored as 1. \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/2-initials.js b/Sprint-2/1-key-exercises/2-initials.js index 964c9563c..46c126832 100644 --- a/Sprint-2/1-key-exercises/2-initials.js +++ b/Sprint-2/1-key-exercises/2-initials.js @@ -1,10 +1,7 @@ const firstName = "Creola"; const middleName = "Katherine"; const lastName = "Johnson"; +const initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; -// Declare a variable called initials that stores the first character of each string. -// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. +console.log(initials); -const initials = ``; - -// https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-2/1-key-exercises/3-paths.js b/Sprint-2/1-key-exercises/3-paths.js index ab90ebb28..7b6d6b9c4 100644 --- a/Sprint-2/1-key-exercises/3-paths.js +++ b/Sprint-2/1-key-exercises/3-paths.js @@ -17,7 +17,11 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir = filePath.slice(0, lastSlashIndex); +console.log(`The dir part of ${filePath} is ${dir}`); + +const ext = base.slice(4); +console.log(`The ext part of ${filePath} is ${ext}`); + // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-2/1-key-exercises/4-random.js b/Sprint-2/1-key-exercises/4-random.js index 292f83aab..bf58fafae 100644 --- a/Sprint-2/1-key-exercises/4-random.js +++ b/Sprint-2/1-key-exercises/4-random.js @@ -2,8 +2,11 @@ const minimum = 1; const maximum = 100; const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; +console.log(num); -// In this exercise, you will need to work out what num represents? -// Try breaking down the expression and using documentation to explain what it means -// It will help to think about the order in which expressions are evaluated -// Try logging the value of num and running the program several times to build an idea of what the program is doing + +// num variable that carries the result of the expressions evaluated. +// (maximum - minimum) does basic math: 100 - 1 + 1 = 100 +// Math.floor() takes out all decimal and make them an integer. +// Math.random() gives random numbers between 0 and 100. +// + minimum add 1 at the end but randomise to the value between 0 and 100 \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/0.js b/Sprint-2/2-mandatory-errors/0.js index cf6c5039f..d5de77529 100644 --- a/Sprint-2/2-mandatory-errors/0.js +++ b/Sprint-2/2-mandatory-errors/0.js @@ -1,2 +1,5 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +// This is just an instruction for the first activity - but it is just for human consumption +// We don't want the computer to run these 2 lines - how can we solve this problem? + + +// This error is a syntaxError because javaScript could not understand the text. The error can be solved by commenting the lines using ("//") \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/1.js b/Sprint-2/2-mandatory-errors/1.js index 7a43cbea7..455d2f3ae 100644 --- a/Sprint-2/2-mandatory-errors/1.js +++ b/Sprint-2/2-mandatory-errors/1.js @@ -1,4 +1,8 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; +console.log(age); + +// This code returns a "TypeError: Assignment to constant variable.", because the variable age has been made constant and can't be manipulated. +// To solve the problem, we have to reassign the age variable from "const" to "let", that way it will allow for further manipulation and solve the error. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/2.js b/Sprint-2/2-mandatory-errors/2.js index e09b89831..be5e8e8c1 100644 --- a/Sprint-2/2-mandatory-errors/2.js +++ b/Sprint-2/2-mandatory-errors/2.js @@ -1,5 +1,9 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + + +// In this task, the variable was not assigned before attempting to print. Therefore, it gives the error "ReferenceError: Cannot access 'cityOfBirth' before initialization" +// The solution would be to assign the variable "cityOfBirth" first before printing the code. \ No newline at end of file diff --git a/Sprint-2/2-mandatory-errors/3.js b/Sprint-2/2-mandatory-errors/3.js index ec101884d..2f163ffd5 100644 --- a/Sprint-2/2-mandatory-errors/3.js +++ b/Sprint-2/2-mandatory-errors/3.js @@ -1,9 +1,8 @@ -const cardNumber = 4533787178994213; +const cardNumber = "4533787178994213"; const last4Digits = cardNumber.slice(-4); +console.log(last4Digits) -// The last4Digits variable should store the last 4 digits of cardNumber -// However, the code isn't working -// Before running the code, make and explain a prediction about why the code won't work -// Then run the code and see what error it gives. -// Consider: Why does it give this error? Is this what I predicted? If not, what's different? -// Then try updating the expression last4Digits is assigned to, in order to get the correct value +// The variable cardNumber is not a string and the slice() function works with strings or array + +// Consider: Why does it give this error? +// It gave that error "TypeError: cardNumber.slice is not a function" brcause the slice() function tool are not applicable to numbers. diff --git a/Sprint-2/2-mandatory-errors/4.js b/Sprint-2/2-mandatory-errors/4.js index 5f86c730b..52110e8cf 100644 --- a/Sprint-2/2-mandatory-errors/4.js +++ b/Sprint-2/2-mandatory-errors/4.js @@ -1,2 +1,6 @@ -const 12HourClockTime = "8:53pm"; -const 24hourClockTime = "20:53"; +const HourClockTime1 = "8:53pm"; +const hourClockTime2 = "20:53"; +console.log(`${HourClockTime1}, ${hourClockTime2}`) + +// The code displayed the error "SyntaxError: Invalid or unexpected token", this is because javaScript does not understand the variable name format. +// This error is solved by renaming the variable name starting it with an alphabet and avoiding starting the variable with a number. diff --git a/Sprint-2/3-mandatory-interpret/1-percentage-change.js b/Sprint-2/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..94eed36ef 100644 --- a/Sprint-2/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-2/3-mandatory-interpret/1-percentage-change.js @@ -2,7 +2,7 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -13,10 +13,23 @@ console.log(`The percentage change is ${percentageChange}`); // a) How many function calls are there in this file? Write down all the lines where a function call is made +// Answer: Line 4 has 2 function call :'Number(), replaceAll()'. +// Line 5 has 2 function call: 'Number(), replaceAll()'. +// Line 10 has 1 function call console.log() +// In total there are 5 function calls. + // b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem? +// Answer: The error is coming from line 5, spotting at the Number(priceAfterOneYear.replaceAll("," "")); +// It's a "SyntaxError: missing ) after argument list", missing a comma inside the replaceAll.() function. // c) Identify all the lines that are variable reassignment statements +// Answer: Line 4 and 5 are variable assignment statements + // d) Identify all the lines that are variable declarations +// Answer: Line 1,2,7 and 8 are variable declearations + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// Answer: ReplaceAll() function clears out all the commas in the strings, the number() converts the strings to numbers to help with calculation diff --git a/Sprint-2/3-mandatory-interpret/2-time-format.js b/Sprint-2/3-mandatory-interpret/2-time-format.js index 47d239558..c730783ce 100644 --- a/Sprint-2/3-mandatory-interpret/2-time-format.js +++ b/Sprint-2/3-mandatory-interpret/2-time-format.js @@ -13,13 +13,27 @@ console.log(result); // a) How many variable declarations are there in this program? +// Answer: There is only 6 variable declearation altogether in this program + // b) How many function calls are there? +// Answer: There is only 1 call function in this program console.log() + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Answer: The % symbol represent the remainder (modulo) operator, it calculates the leftover/remainder after integer division. +// Answer: % 60 divides the movieLength by 60 and returns only the remainder. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Answer: Line 4 expression helps to eliminate any remainder from the equation, so its left with a clean multiple of 60 to avoid any decimal + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Answer: It represent the total duration of the movie formatted into hours, minutes and seconds +// Answer: A better name could be "totalMovieDuration" + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// Answer: It will work for integer values and will give us clear and clean result using this code. However, the result would not be clean as this using float values diff --git a/Sprint-2/3-mandatory-interpret/3-to-pounds.js b/Sprint-2/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..c798b315a 100644 --- a/Sprint-2/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-2/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,35 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// Answers: In line 1, a variable "penceString" was created holding the value "399p" + +// In line 3-6, Another variable "penceStringWithoutTrailingP" was created to remove the trainling "p". +// That is, taking off the "p" in '399p' leaving it at just "399". + +// Also, line 3-6 used the function substring(0, penceString.lrength - 1) to cut out the "p" in the "penceString" variable. +// By starting count fron 0 (beginnning) of "399p". +// "Using penceString.lenght - 1" to check the length of the value in the variable penceString = "399p" (4), +// And deducting 1 from the value (4 - 1), because length - 1 subtract 1 from the character/value count 4. leaving the value at "399" + +// Line 8 a variable "paddedPenceNumberString" was created and a function padStart(3, "0") +// was used to ensure the value of the variable remains at 3 and to be filled with "0" at the beginning if the value is less 3 + +// Line 9-12 created a variable "pounds" and used the subString(0, paddedPenceNumberString.length - 2 ) +// to start count from 0 of the "penceStringWithoutTrailingP" value which is 399. +// It further removed the last two number in the value by checking the length of the value with "paddedPenceNumberString.length" +// And subtracting 2 (3 - 2), because length - 2 subtract 2 from the character/value count. Cutting out 99 and leaving the value to remain 3. + +// Line 14-16 created a variable "pence", using the subString(paddedPenceNumberString.length - 2) +// to check the length of the value of the variable "paddedPenceNumberString" which remain 399 from line 8 +// The "substring(paddedPenceNumberString.length - 2)" function deducts 3 leaving the last two numbers 99 +// PadEnd(2, "0") ensure the value remains 2 and 0 to be added to value less than 2. +// However, we already have in our "pence" variable at a value of 2 numbers "99" + +// Line 18 printed our codes a formatted style adding the pound sign "£" + + + + + + diff --git a/Sprint-2/Prep/Hello_World.js b/Sprint-2/Prep/Hello_World.js new file mode 100644 index 000000000..e69de29bb diff --git a/Sprint-2/Prep/facts.js b/Sprint-2/Prep/facts.js new file mode 100644 index 000000000..01f81f418 --- /dev/null +++ b/Sprint-2/Prep/facts.js @@ -0,0 +1,2 @@ +console.log("One fact i love about git is that it allows people to collaborate on projects"); + diff --git a/Sprint-2/Prep/functions.js b/Sprint-2/Prep/functions.js new file mode 100644 index 000000000..3d7029f20 --- /dev/null +++ b/Sprint-2/Prep/functions.js @@ -0,0 +1,66 @@ +console.log("In this exercise I will be learning different functions, and preactising them. "); + +let userName = "Matthew"; +let location = "Manchester"; +let favoriteFood = "Jollof rice"; + +function myInfo(userName, location, favoriteFood) { + console.log(`My name is ${userName}` ) + console.log(`I live in ${location}` ) + console.log(`My favorite food is ${favoriteFood}` ) +}; +myInfo(userName, location, favoriteFood); + +sliceUserName = userName.slice(0, 4); +console.log(`My friends love to call me ${sliceUserName}`); + +sliceLocation = location.lastIndexOf("h"); +console.log(sliceLocation); + +const number1 = "50"; +const number2 = "100"; +const result = Number(number1) + Number(number2); +console.log(result); + +let hobby = "I love football and traveling"; +let moreHobby = hobby.replaceAll("football", "swiming").replaceAll("traveling", "hiking"); +console.log(moreHobby); + +const passion = hobby.substring(6, 15); +console.log(passion); + +const luckyNumber = "7"; +const changeNumber = luckyNumber.padStart(5, "0"); +console.log(changeNumber); + +const anotherNumber = luckyNumber.padEnd(5, "0"); +console.log(anotherNumber); + +const carPrice = 30000.50; +const updatedCarPrice = Math.floor(carPrice); +console.log(updatedCarPrice); + +const addNumber = 20; +const addNumber2 = 30; +const addNumber3 = addNumber + addNumber2; +const addNumberResult = Math.random() * addNumber3 +console.log(addNumberResult); + +const repeatedText = "one, two, three"; +const lastOnePosition = repeatedText.lastIndexOf("one"); +console.log(lastOnePosition); + +// const yourName = prompt("Please enter your name: "); +// const yourLocation = prompt("Please enter your location: "); +// const yourHobby = prompt("Enter your hobby: "); +// const yourFavoriteFood = prompt("Please enter your favorite food: ") + +// function userInfo(yourName, yourLocation, yourHobby, yourFavoriteFood) { +// console.log(`Your name is ${yourName}`) +// console.log(`You live in ${yourLocation}`) +// console.log(`Your hobby is ${yourHobby}`) +// console.log(`Your favorite food is ${yourFavoriteFood}`) +// } + +// userInfo(yourName, yourLocation, yourHobby, yourFavoriteFood) +// Chrome devOps prompt function, variable and parameters diff --git a/Sprint-2/Prep/greeting.js b/Sprint-2/Prep/greeting.js new file mode 100644 index 000000000..c41f3aee1 --- /dev/null +++ b/Sprint-2/Prep/greeting.js @@ -0,0 +1,34 @@ +const greeting = "Hello There !"; +console.log(greeting); + +const name = "Matt"; +console.log(`${greeting}, I am ${name}`); + +let location = "Manchester"; +console.log(`${greeting}, my name is ${name} and i live in ${location} `); + +let location2 = "London"; +console.log(`${greeting}, I am ${name}, I will be traveling to ${location2} soon`); + +console.log("Dog" === "Mango"); + +console.log(5 + 15 === 20); + +console.log("dog" == "dog"); + +console.log(40+5 == "45"); + +console.log(60+40 > 50); + +console.log(100<= 90); + + + + + + + + + + + diff --git a/Sprint-2/Prep/password.js b/Sprint-2/Prep/password.js new file mode 100644 index 000000000..cf6d59c07 --- /dev/null +++ b/Sprint-2/Prep/password.js @@ -0,0 +1,16 @@ +const password = "Matt123"; +const userInput = "Anything"; +const administrator = "Anything"; +let response = " "; + +if (password === userInput) { + response = "Login successful"; +} else if (userInput === administrator) { + response = "Welcome to the site"; +} +else { + response = "Please try again"; +} +console.log(response); + + diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..552419761 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... -// =============> write your prediction here +// ======> write your prediction here: I predict that the code should cut out, and make the first letter of the string an uppercase letter, +// then add every letters that comes after // 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: The error read "SyntaxError: Identifier 'str' has already been declared", +// this is as a result of having two variables with same name. The error occoured in this code because the variable "str" has been decleared twice -// =============> write your explanation here // =============> write your new code here + +function capitalise(str) { + let str2= `${str[0].toUpperCase()}${str.slice(1)}` + return str2; +} +console.log(capitalise("money")) \ No newline at end of file diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..6f0b04f61 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,20 +1,38 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// Answer below: +// A syntax error will occur for two reasons, first because there variable const "decimalNumber" being redecleared +// The second reason is because the function "convertToPercentage" is not called correctly. -// Try playing computer with the example to work out what is going on +// =============> write your prediction here: +// Answer below: +// I predicted that the code will not work because of the variable (decimalNumber) inside the function, +// Also because the console.log(decimalNumber) is just a name that exist inside the function -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// Try playing computer with the example to work out what is going on + +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; -console.log(decimalNumber); +// return percentage; +// } +// console.log(decimalNumber); // =============> write your explanation here +// Answer: + +// const decimalNumber redeclares the parameter name which causes a SyntaxError. +// also console.log(decimalNumber) outside fails because the parameter only exists inside 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(10.5)); \ No newline at end of file diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..3c5f20756 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,31 @@ // Predict and explain first BEFORE you run any code... +// Answer below: +// The code will not work because the placeholder variable (3) should be an actual name like "num" + // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// The error message explains the error in the function parameter (3), the function expects a name parameter instead of a number. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} +console.log(square(10)) + + diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..9216f69ec 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... +// Answer below: +// The code needs a value inside the console.log function to multiply, but no value was useed to replace the placeholder parameters +// It should be an error, no value to return // =============> write your prediction here -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 +// Answer below: +// The original logs the product instead of returning it, so the function call inside the template string gives 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)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..800cf59a7 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// Code will not work because the paremeters should be on same line as return -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 errors in this codes is the semi-colon in front of the return and the placeholder parameters not on same line as the return // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..4571f53fa 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,23 +2,48 @@ // Predict the output of the following code: // =============> Write your prediction here +// Answer below: +// There is a constant variable num with value 103, the code should print last digit of the constant variable. -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 +// Answer below: +// 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 +// Answer below: +// The code kept repeating same value for all log because of the constant variable decleared before the function and used inside 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)}`); + + // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem + +// Answer below: + +// The global constant variable outside the function has affected the code and caused it to always reference the last number of the global variable +// To fix this, getLastDigit needed its own parameter, so num comes from whatever you pass in \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..4b6398b12 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,8 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { + const bmi = weight / (height * height) + return bmi.toFixed(1) // return the BMI of someone based off their weight and height } +console.log(`Your BMI is ${calculateBMI(100, 1.62)}`) diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..b404d1ea0 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,9 @@ // 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 upperCase(str) { + const returnUpperCase = str.toUpperCase().replaceAll(" ", "_") + return returnUpperCase +} +console.log(upperCase("what is your name mr man? ")) \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..2ca524252 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,43 @@ // 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 + + +// const penceString = "399p"; + +// 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"); + +// console.log(`£${pounds}.${pence}`); + + +function toPounds(inPounds) { + const penceStringWithoutTrailingP = inPounds.substring( + 0, + inPounds.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("599p")) \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..58c1dc52b 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -1,11 +1,15 @@ function pad(num) { + // console.log(num) let numString = num.toString(); + // console.log(numString, "numstr") while (numString.length < 2) { numString = "0" + numString; } + // console.log(numString, "FULLNUM") return numString; } + function formatTimeDisplay(seconds) { const remainingSeconds = seconds % 60; const totalMinutes = (seconds - remainingSeconds) / 60; @@ -14,6 +18,10 @@ 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 @@ -21,18 +29,27 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here +// Answer below: +// Pad runs once for "totalHours", once "remainingMinutes" and once for "remainingSeconds" in total 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 +// =============> write your answer here +// Answer below: +// The first pad is pad(totalHours) and the value is 0 // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// The value is 0 plus the "0" added to it to make the value 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 +// =============> write your answer here +// Answer below: +// The last call is pad(remainingSeconds) and the value is 1 // 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 +// =============> write your answer here +// Answer below: +// the return value initially is 1 and "0" was added to it in front to make the value 01 diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..d7a71c282 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -2,24 +2,129 @@ // 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`; +// } +// // console.log(formatAs12HourClock("10:15")); + +// // Early Morning: ================> assertion passed +// let currentOutput = formatAs12HourClock("08:30"); +// let targetOutput = "08:30 am"; +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ); +// console.log(formatAs12HourClock("08:30")) + +// // Mid-morning ==============> assertion passed +// currentOutput = formatAs12HourClock("10:00"); +// targetOutput = "10:00 am"; +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ); +// console.log(formatAs12HourClock("10.00")) + +// // Late morning ============> assertion passed +// currentOutput = formatAs12HourClock("11:45"); +// targetOutput = "11:45 am"; +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ); +// console.log(formatAs12HourClock("11.45")) + + +// // Afternoon =============> assertion failed +// let currentOutput2 = formatAs12HourClock("12:00"); +// let targetOutput2 = "12:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); +// console.log(formatAs12HourClock("12:00")) + +// // Early afternoon =============> assertion passed +// currentOutput2 = formatAs12HourClock("14:00"); +// targetOutput2 = "02:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); +// console.log(formatAs12HourClock("14:00")) + +// // Mid afternoon =============> time display doesn't correspond +// currentOutput2 = formatAs12HourClock("15:30"); +// targetOutput2 = "03:30 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); +// console.log(formatAs12HourClock("15:30")) + +// // Towards evening ==============> time display doesn't correspond +// currentOutput2 = formatAs12HourClock("17:30"); +// targetOutput2 = "05:30 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); +// console.log(formatAs12HourClock("17:30")) + +// // Late night =============> assertion passed +// currentOutput2 = formatAs12HourClock("23:00"); +// targetOutput2 = "11:00 pm"; +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ); +// console.log(formatAs12HourClock("23:00")) + + +// Test result: +// Hours and Minutes worked in the AM function for different time test. +// 12:00 in the afternoon doesn't function correctly +// 00:00 mid-night doesnt work +// Minutes in the PM are not working. + +// To fix bug: + function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am` + } + if (hours === 12) { + return `12:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; } + + return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("01:00"); +const targetOutput = "01:00 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}` ); +console.log(formatAs12HourClock("01:00")) + +const currentOutput2 = formatAs12HourClock("19:30"); +const targetOutput2 = "7:30 pm"; -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +console.log(formatAs12HourClock("19:30"));