diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..0e2794748 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,7 @@ count = count + 1; // 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 + + +// Answer: In line 3 we are redeclaring the variable count to count + 1, this will add the value in count by 1 +// ' = ' this is the assignment operator it is assigning the values to the variable count \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..4457c6d5a 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -1,11 +1,14 @@ + +// 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. +// https://www.google.com/search?q=get+first+character+of+string+mdn + let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; -// 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. +let initial = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); +console.log(initial); -let initials = ``; -// https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..206eabb41 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,8 @@ 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); +const lastExt = base.lastIndexOf(".") +const ext = base.slice(lastExt + 1); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..0ef374937 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,23 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // 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 + + +/* +Answers: I have noticed that num represents a random number between 1 and 100 which is the minimum and maximum +i have broken down the code and tested it one by one +*/ + +console.log(Math.random()); +//this will pick a random decimal number between 0 and 99 +console.log(Math.random() * (maximum - minimum + 1)); +//Mat.random produces a random number then its multiplied by the result we get from maximum - minimum + 1 +// we add 1 to the result making sure that it reaches the maximum value which is 100 +console.log(Math.floor(Math.random() * (maximum - minimum + 1))); +// here with Math.floor added we round down the result to the nearest number +console.log(Math.floor(Math.random() * (maximum - minimum + 1)) + minimum); +// + minimum creates a starting point for the number generated +console.log(num); + + + diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..d49dcab32 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,9 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; -age = age + 1; +// const age = 33; +// age = age + 1; + +let myAge = 30; +myAge = myAge + 1; + +console.log(myAge); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..dea568bd9 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,8 @@ // 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}`); +// there was a hoisting error the variable was only initialized after the console.log so i moved the declaration to the top + const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..c4aab6280 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,9 +1,14 @@ -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 + +/* Answers: cardNumber is not a string or array and the slice method wont work +*/ diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..47ba8e966 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,2 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const HourClockTime24 = "20:53"; +const hourClockTime12 = "08:53"; \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..1f508abe3 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -1,8 +1,8 @@ let carPrice = "10,000"; let priceAfterOneYear = "8,543"; -carPrice = Number(carPrice.replaceAll(",", "")); -priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," "")); +carPrice = Number(carPrice.replaceAll("," , "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," , "")); const priceDifference = carPrice - priceAfterOneYear; const percentageChange = (priceDifference / carPrice) * 100; @@ -12,11 +12,16 @@ console.log(`The percentage change is ${percentageChange}`); // Read the code and then answer the questions below // a) How many function calls are there in this file? Write down all the lines where a function call is made +// Answer: i see four function calls that is line 4, 5, and 10 // 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: line 4 and 5 there was a syntax error with the double quotes and comma // c) Identify all the lines that are variable reassignment statements +// Answer: there is line 4 and 5 // d) Identify all the lines that are variable declarations +// Answer: 1, 2, 7 and 8 // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// Answer: this is to remove all the commas carPrice.replaceAll(",","") and Number converts the string to a number so this expression converts the string into a number \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..68af8a086 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 108784; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -12,14 +12,20 @@ console.log(result); // For the piece of code above, read the code and then answer the following questions // a) How many variable declarations are there in this program? +// Answer: there a six, there is movieLength, remainingSeconds, totalMinutes, remaingMinutes, totalHours and results and they are declared by the const keyword // b) How many function calls are there? +// Answer: there are no functions in this code // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Answer: we are calculating the remainder of movieLength by 60 using the modulus "&" operator // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// Answer: we subtracting the seconds from the movie length '8784' then divide by 60 to get total minutes then you get 146min 4sec // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Answer: this will hold the time in hours minutes and seconds and it will format it, we can call it duration // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// Answer: yes it is working with different values because the arithmetic calculator is set correctly \ No newline at end of file diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..a579bc60e 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -1,20 +1,11 @@ const penceString = "399p"; -const penceStringWithoutTrailingP = penceString.substring( - 0, - penceString.length - 1 -); +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"); +const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2); +const pence = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2).padEnd(2, "0"); console.log(`£${pounds}.${pence}`); // This program takes a string representing a price in pence @@ -23,5 +14,17 @@ console.log(`£${pounds}.${pence}`); // You need to do a step-by-step breakdown of each line in this program // Try and describe the purpose / rationale behind each step + + // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" + +// line 3 extracting part of penceString and assigning it to penceStringWithoutTrailingP , 0 is the starting point and -1 is the last character so in this case "p" and will get 399 + +// line 5 we adding 3 zeroes to the start of the string and assigning it to paddedPenceNumberString but they wont be added since we already have 3 characters + +// line 6 extract the first 2 characters of the string from paddedPenceNumberString and assigns it to pounds + +// line 8 it extracts the last 2 characters and makes sure there will always be 2 + +// line 9 displays the result of the code \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..c16533d70 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,20 @@ // Predict and explain first... // =============> write your prediction here +//Answer: this will capitalize the first character of the string + // 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; -} +// Answer : this was the error SyntaxError: Identifier 'str' has already been declared + // =============> write your explanation here +// variable let was redeclared in the code so it was causing "Identifier 'str' has already been declared" error so will remove the let keyword // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; + }; +console.log(capitalise("welcome")); \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..a980a03a8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,18 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// code wont run since the parameter will not take any other value, it was declared already as 0.5 so we need to remove "const decimalNumber = 0.5;" from our code // Try playing computer with the example to work out what is going on +// =============> write your explanation here +// our parameter has been overwritten so it will always take the same value and nothing else and the variable has been declare outside its scope since it can only be accessed inside the function +// Finally, correct the code to fix the problem +// =============> write your new code here + function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +console.log(convertToPercentage(2)); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..2bfb2e22d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,5 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + } \ No newline at end of file