diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..36a9843e8 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -1,6 +1,55 @@ -let count = 0; +//let count = 0; -count = count + 1; +//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 +// Line 3 returns the incremented value of count. The operator = assigns count +1 to the varibale count in the +// left. + +// Described what line three is doing. + +// Example from codewar (from programming workshop) + +function drawStairs(n) { + let result = ""; + if (n < 1) return result; // nothing to do + + let iCount = 0; + while (iCount < n) { + // add spaces before the I (for all lines after the first) + let spaceCount = 0; + while (spaceCount < iCount) { + result = result.concat(" "); + spaceCount = spaceCount + 1; + } + + // add the "I" + result = result.concat("I"); + + // add a newline after each line except the last one + if (iCount < n - 1) { + result = result.concat("\n"); + } + + // update the count + iCount = iCount + 1; + } + + return result; +} + +console.log(drawStairs(10)); + +/// Another example + +function drawStairs(n) { + let result = []; + + for (let i = 0; i < n; i++) { + result[i] = `${' '.repeat(i)}I`; + } + + return result.join('\n'); +} +console.log(drawStairs(5)); diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..c65df8273 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,17 @@ 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 initials = ``; +//let initials = firstName[0] + middleName[0] + lastName[0]; + +let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`; + +console.log(initials) // https://www.google.com/search?q=get+first+character+of+string+mdn +// Declared a variable named initials + +// Used template string to display initials Line 10 + + + diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..e402782c7 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(lastSlashIndex+1); +const ext = base.slice(base.lastIndexOf(".") + 1); +console.log(dir) // 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..bf468194c 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,18 @@ 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 + + +// num represents the result of equation at the right hand side of the operator = + +// Step-1 ---> Math.random() generates a random decimal, +// Step-2 ---> (maximum-minumum +1) is evaluated +// Step-3 ---> Math.random() is multiplied by (maximum-minumum +1) +// Step-4 ---> the resukt of Step-3 is round by the method Math.floor +// Step-5 ---> minimum is added to the result of Step-4 + +// Thus, num represents random numbers generated between minimum 1 and maximum 100. As we run the code several times it +// generates new numbers. + + +console.log(num); \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..bf619b422 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/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? + +// We have solved it by adding two forward slashes (//) at the beginning of each line. +// The computer does not run this line too. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..cd331def8 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,14 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +//const age = 33; + +// we need to replace "const" with "let", in javascript "const" is usd for variable +// that will nor change / will be kept constant troughout the code, where is "let" +// lets us to reassign variable later as we have done in the lines below the variabel age +// gets updated by one... + +let age = 33; age = age + 1; + + +console.log(age); \ 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..a624c246f 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/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}`); +// The variable cityOfBirth was called before it was defined, so we need to bring that variable first const cityOfBirth = "Bolton"; +console.log(`I was born in ${cityOfBirth}`); + +// Problem has been fixed + diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..43671ab62 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,10 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +//const last4Digits = cardNumber.slice(-4); + +const last4Digits = cardNumber.toString().slice(-4); + + +console.log(last4Digits); // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +12,9 @@ const last4Digits = cardNumber.slice(-4); // 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 + +// I think the code does not work because cardNumber variable is not stored as string. + +// My guess was right. + +// Therefore first we need to change CardNumber variable into string before we use the slice method. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..6c8ed1ed0 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,14 @@ const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const 24hourClockTime = "08:53"; + +// Alternatively the above variable names can be corrected as follows: + +// Option-1 + +const twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "08:53"; + +// Option -2 + +const hour12ClockTime = "20:53"; +const hour24ClockTime = "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..975f4a740 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/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; @@ -12,11 +12,23 @@ 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 + +// Ans: There are five (5) function calls, Line 4 Number() and replaceAll(), Line 5, Number() and replaceAll() + // Line 10 log(). // 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? +// Ans: The error is coming from line 5 "("," ""));"" a comma was missing. Putting a comma solved the problem. + // c) Identify all the lines that are variable reassignment statements + +// Ans: Line 4 carPrice and Line 5 priceAfterOneyear are variable reassignments. // d) Identify all the lines that are variable declarations +// Ans: 4 variable declaration Line 1, Line 2, Line 7 and Line 8 + // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +// Ans: The purpose of the expression Number(carPrice.replaceAll(",","")) is to change a string argument the contains commas passed to replaceAll() +// function into number, first the function replaceAll() removes the commas and then the function Number() changes it into number. diff --git a/Sprint-1/3-mandatory-interpret/2-time-format.js b/Sprint-1/3-mandatory-interpret/2-time-format.js index 47d239558..ff8df7259 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 = -3500; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -13,13 +13,37 @@ console.log(result); // a) How many variable declarations are there in this program? +// Ans: There are 6 variable declaration: Line 1, Line 3, Line 4, Line 6, Line 7, Line 9, + // b) How many function calls are there? +// Ans: There is one function call ... log() + // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// Ans: The expression movieLength % 60 returns the remainder after totalMinutes is divided by 60. + // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The expression is calculating how many full minutes are in the movie’s total length by removing leftover seconds(less than 60) + // e) What do you think the variable result represents? Can you think of a better name for this variable? +// Ans: the variable result represent total movie lenght, Movie duration_hr_min_sec would be better name. + +// In a "camel case" the following options would be better names +// 1. movieDurationHrMinSec +// 2. totalMovieSpan +// 3. totalMoviePeriod + // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer + +// Ans: I have tried with different values and it seems to work for all values, but it needs to validate to avoid entering negative values +// I inserted --3500 and returned 0: -58: -20 which does not mean real time representation. + + +// Corrected typo reminder -----> remainder +// Suggested better names in "camel case" a convention in Javascript + +// corrected name suggestion according to camelCase diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..3782a9ae5 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -25,3 +25,14 @@ console.log(`£${pounds}.${pence}`); // To begin, we can start with // 1. const penceString = "399p": initialises a string variable with the value "399p" +// 2. const penceStringWithoutTrailingP = a string value "399" is reassigned to penceStringWithoutTrailingP after + // the the letter p is removed from "399p" +// 3. const paddedPenceNumberString = declared new variable, padStart ensures any argument passed is at least 3 digits long. +// 4. Line 9 a new variable pence is declared and its value comes from the padded string by removing the last two strings. + // eg. 399 removing 99 get 3 if the intial amount was 55 after padding it would be 055 and the pound result would be 0 + +// 5. Line 14 const pence = declared a variable and the variable is the result after extracts the last two digits + // from the pence string and pads it to 2 characters to have two digit pence string. + +// 6. Line 18- console.log(`£${pounds}.${pence}`); returns money amount combining pound and pence alog with the symbol £. +// eg. £3.99 \ No newline at end of file