From 1c7d5582400624faa4929aca2e0f9651a40a5cd8 Mon Sep 17 00:00:00 2001 From: ofonimeedak Date: Wed, 25 Feb 2026 11:20:14 +0000 Subject: [PATCH] sprint1 solutions --- Sprint-1/1-key-exercises/1-count.js | 2 ++ Sprint-1/1-key-exercises/2-initials.js | 3 +-- Sprint-1/1-key-exercises/3-paths.js | 4 ++-- Sprint-1/1-key-exercises/4-random.js | 9 +++++++++ Sprint-1/2-mandatory-errors/0.js | 4 ++-- Sprint-1/2-mandatory-errors/1.js | 2 +- Sprint-1/2-mandatory-errors/2.js | 7 +++++-- Sprint-1/2-mandatory-errors/3.js | 7 ++++++- Sprint-1/2-mandatory-errors/4.js | 4 ++-- .../1-percentage-change.js | 13 ++++++++++++- .../3-mandatory-interpret/2-time-format.js | 11 +++++++++++ Sprint-1/3-mandatory-interpret/3-to-pounds.js | 18 +++++++++++------- Sprint-1/4-stretch-explore/chrome.md | 7 +++++++ Sprint-1/4-stretch-explore/objects.md | 11 ++++++++++- 14 files changed, 81 insertions(+), 21 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..60f9a3319 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,5 @@ 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 of the code assigns a value to the declared variable count. +// The = sign is an assignment operator, without line 3, the declared variable wil be undefined. diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..dc937f31d 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -5,7 +5,6 @@ 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}+${lastName}`; // 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..9ff20cdf2 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ 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,filePath.length-base.length-1) ; +const ext = filePath.slice(-3); // 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..13b9f532d 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,12 @@ 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 + +// In the above expression num represent a variable that will store the result of that will be obtained from from the expression. +// maximum and minimum are variables available on the global scope, as inputs. +// They are evaluated first because of the parenthesis, this gives the precedence +// The output from the parenthesis is now going to serve as the range for the Math.random() method. +// Math.random() method produce random values from 0-1, with 0 and 1 inclusive and exclusive respectively +// The range value makes Math.random() to return result from 0 to range-1 +//the Math.floor() methods rounds down the value to the nearest whole number. +// The result from th random operation is added back to the minimum value to return 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..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -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? \ 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..031839b47 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -1,4 +1,4 @@ // trying to create an age variable and then reassign the value by 1 -const age = 33; +let age = 33; age = age + 1; diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..9731cfc43 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 ? + +// JavaScript is single-threaded because it executes tasks in a single flow using a call stack. The function was called before it was declared, +// It was also declared with const, which can not be hoisted to the top, like var. +// this result to undefined. -console.log(`I was born in ${cityOfBirth}`); 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..321ffa67c 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,4 +1,5 @@ -const cardNumber = 4533787178994213; +let cardNumber = 4533787178994213; +cardNumber=cardNumber.toString(); const last4Digits = cardNumber.slice(-4); // The last4Digits variable should store the last 4 digits of cardNumber @@ -7,3 +8,7 @@ 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 + +// The variable cardNumber is a number data type, numbers do not have slice methods or function, only string does. +//TypeError: cardNumber.slice is not a function +// Yes, it gives what i predicted \ 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..f0ab94e12 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 twelveHourClockTime = "20:53"; +const twentyFourHourClockTime = "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..59b4c67ff 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; @@ -13,10 +13,21 @@ 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 +//There are 5 function call in this code. +// 1. Line 4 has 2 function call, .replaceAll() and Number() +//2. Line 5 ditto line 4 +// 3. line 10 console.log() that logs the result to the console + // 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? +// The error in this code is coming from line 5. The wrong use of the replaceAll syntax without a separator. +// I will fix this problem by reading the documentation, to see the right way the syntax should be written. +//Then i will add the comma where necessary // c) Identify all the lines that are variable reassignment statements + // line 4 and 5 are variable reassignment // d) Identify all the lines that are variable declarations + // line 1,2,7,8 are all variable declaration // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// this expression replaces the comma at the string with an empty string and the convert the string data type to a number data type \ 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..5b1f7dd90 100644 --- a/Sprint-1/3-mandatory-interpret/2-time-format.js +++ b/Sprint-1/3-mandatory-interpret/2-time-format.js @@ -12,14 +12,25 @@ 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? +// there are 6 variable declaration. + // b) How many function calls are there? +// One function call // c) Using documentation, explain what the expression movieLength % 60 represents // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +// The % operator checks the remainder of that expression when being evaluated. It is similar to the remainder theorem in mathematics. +// if it returns 0, even, if it returns value other than zero, it odd. it is mostly used for checks in programming // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// The total time for this movie is 8760 second and fractional 24 seconds, line 4 is used to deduct the fractional 24 seconds +// Then convert the whole number seconds to minute. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// the variable result represent total movie duration in hours,minute and seconds. +// const=movieLength // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +// this code works for all movie length and integer values. +// for movie length which are multiples of 60 it returns hours with 0 minutes and 0 seconds diff --git a/Sprint-1/3-mandatory-interpret/3-to-pounds.js b/Sprint-1/3-mandatory-interpret/3-to-pounds.js index 60c9ace69..13a02faf9 100644 --- a/Sprint-1/3-mandatory-interpret/3-to-pounds.js +++ b/Sprint-1/3-mandatory-interpret/3-to-pounds.js @@ -6,14 +6,9 @@ const penceStringWithoutTrailingP = penceString.substring( ); const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); -const pounds = paddedPenceNumberString.substring( - 0, - paddedPenceNumberString.length - 2 -); +const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2); -const pence = paddedPenceNumberString - .substring(paddedPenceNumberString.length - 2) - .padEnd(2, "0"); +const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); console.log(`£${pounds}.${pence}`); @@ -25,3 +20,12 @@ 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 = penceString.substring(0,penceString.length - 1);: used to strip off the last substring character 'p' +// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Used to add '0' to the start of the string if the string +// length is less than 3, else if string length is 3 it does not pad. +// 4.const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2);: This expression copy the first character +// from every length character of the padded string +// 5.const pence = paddedPenceNumberString .substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: This expression add '0' to the end if the paddedString is not +// upto 2 characters +// 6. this line is a function call to log the argument inside it to the console. it use template literals to get the values of the variables + diff --git a/Sprint-1/4-stretch-explore/chrome.md b/Sprint-1/4-stretch-explore/chrome.md index e7dd5feaf..297190d36 100644 --- a/Sprint-1/4-stretch-explore/chrome.md +++ b/Sprint-1/4-stretch-explore/chrome.md @@ -10,9 +10,16 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; +It invokes the modal window and display Hello world + What effect does calling the `alert` function have? +It freezes the window till i click Ok before ii can make use of my computer Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`. +const myName=prompt(`"What is your name?"`) +return value myName=Edak What effect does calling the `prompt` function have? +it invokes a modal window, and allows me to enter an input value What is the return value of `prompt`? +it returns the value i entered "Edak" diff --git a/Sprint-1/4-stretch-explore/objects.md b/Sprint-1/4-stretch-explore/objects.md index 0216dee56..65f6ed02d 100644 --- a/Sprint-1/4-stretch-explore/objects.md +++ b/Sprint-1/4-stretch-explore/objects.md @@ -5,12 +5,21 @@ In this activity, we'll explore some additional concepts that you'll encounter i Open the Chrome devtools Console, type in `console.log` and then hit enter What output do you get? +I get `f log(){[native code]}` in return Now enter just `console` in the Console, what output do you get back? + I get`console{debug: f,error:f,info:f,log:f....}` Try also entering `typeof console` - +I get `object` Answer the following questions: What does `console` store? +The console store methods for debugging the console such as assert. clear,count,countReset,debug etc + What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? + +console.log: Output a message to the console + +console.assert: Log an error message to the console if the first argument is false. +`.` console is the class the . helps you to access the methods or properties of the class console.