diff --git a/week-1/errors/0.js b/week-1/errors/0.js index cf6c5039..c8036882 100644 --- a/week-1/errors/0.js +++ b/week-1/errors/0.js @@ -1,2 +1 @@ -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 +console.log("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/week-1/errors/1.js b/week-1/errors/1.js index 7a43cbea..8c5e17a2 100644 --- a/week-1/errors/1.js +++ b/week-1/errors/1.js @@ -1,4 +1,5 @@ // 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); \ No newline at end of file diff --git a/week-1/errors/2.js b/week-1/errors/2.js index e09b8983..c46e9808 100644 --- a/week-1/errors/2.js +++ b/week-1/errors/2.js @@ -1,5 +1,5 @@ // 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}`); + diff --git a/week-1/errors/3.js b/week-1/errors/3.js index ffa72ca4..5e0c3df8 100644 --- a/week-1/errors/3.js +++ b/week-1/errors/3.js @@ -1,6 +1,6 @@ const cardNumber = 4533787178994213; -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 // Make and explain a prediction about why the code won't work diff --git a/week-1/errors/4.js b/week-1/errors/4.js index 21dad8c5..27b0026a 100644 --- a/week-1/errors/4.js +++ b/week-1/errors/4.js @@ -1,2 +1,3 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file +const twelveHourClockTime = "20:53"; +const twenty_four_hourClockTime = "08:53"; +console.log(twenty_four_hourClockTime + ', ' + twelveHourClockTime); \ No newline at end of file diff --git a/week-1/exercises/count.js b/week-1/exercises/count.js index 117bcb2b..d49978da 100644 --- a/week-1/exercises/count.js +++ b/week-1/exercises/count.js @@ -1,6 +1,5 @@ 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 diff --git a/week-1/exercises/decimal.js b/week-1/exercises/decimal.js index bd4a4740..889e2106 100644 --- a/week-1/exercises/decimal.js +++ b/week-1/exercises/decimal.js @@ -1,6 +1,12 @@ const num = 56.5467; +const wholeNumberPart = Math.floor(num); +const decimalPart = num - wholeNumberPart; +const roundedNum = Math.round(num); +console.log("Whole Number Part:", wholeNumberPart); +console.log("Decimal Part:", decimalPart); +console.log("Rounded Number:", roundedNum); // You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math // Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) diff --git a/week-1/exercises/initials.js b/week-1/exercises/initials.js index 50b62103..1a74e2ce 100644 --- a/week-1/exercises/initials.js +++ b/week-1/exercises/initials.js @@ -1,6 +1,7 @@ let firstName = "Creola"; let middleName = "Katherine"; let lastName = "Johnson"; - +let initials = firstName.charAt(0).toUpperCase()+middleName.charAt(0).toUpperCase()+lastName.charAt(0).toUpperCase(); +console.log(initials); // Declare a variable called initials that stores the first character of each string in upper case to form the user's initials // Log the variable in each case diff --git a/week-1/exercises/paths.js b/week-1/exercises/paths.js index c91cd2ab..ed74aba8 100644 --- a/week-1/exercises/paths.js +++ b/week-1/exercises/paths.js @@ -12,7 +12,10 @@ const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; const lastSlashIndex = filePath.lastIndexOf("/"); const base = filePath.slice(lastSlashIndex + 1); -console.log(`The base part of ${filePath} is ${base}`); +const dir = filePath.slice(0, lastSlashIndex); +const lastDotIndex = filePath.lastIndexOf("."); +const ext = filePath.slice(lastDotIndex + 1); -// Create a variable to store the dir part of the filePath variable -// Create a variable to store the ext part of the variable +console.log(`The base part of ${filePath} is ${base}`); +console.log(`The directory part of ${filePath} is ${dir}`); +console.log(`The extension part of ${filePath} is ${ext}`); diff --git a/week-1/exercises/random.js b/week-1/exercises/random.js index 79a4a4d5..2ce34ae2 100644 --- a/week-1/exercises/random.js +++ b/week-1/exercises/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 several times to build an idea of what the program is doing +// math.random gives a random decimal number +// and 100-1+1 = 100; +// when decimal value multiply with 100 it and math.floor gives a whole no then every this results shows in a different \ No newline at end of file diff --git a/week-1/explore/chrome.md b/week-1/explore/chrome.md index e7dd5fea..16563a6d 100644 --- a/week-1/explore/chrome.md +++ b/week-1/explore/chrome.md @@ -9,10 +9,13 @@ Let's try an example. In the Chrome console, invoke the function `alert` with an input string of `"Hello world!"`; - +A dialog box with message display on top of the page What effect does calling the `alert` function have? +A dialog box with ok button displays with message when user click on ok button the dialog box disappears 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`. - +its works properly I use console.log(nyName); it displays my name that is entered on prompt dialog box What effect does calling the `prompt` function have? +A dialog box is opened on the top of the page with a textbox and to buttons ok and cancel What is the return value of `prompt`? +if user click on ok prompt return a string and if user click on cancel it returns null diff --git a/week-1/explore/objects.md b/week-1/explore/objects.md index 0216dee5..bccf52c4 100644 --- a/week-1/explore/objects.md +++ b/week-1/explore/objects.md @@ -5,12 +5,91 @@ 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? - +'Undefined' Now enter just `console` in the Console, what output do you get back? - +console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} +assert +: +ƒ assert() +clear +: +ƒ clear() +context +: +ƒ context() +count +: +ƒ count() +countReset +: +ƒ countReset() +createTask +: +ƒ createTask() +debug +: +ƒ debug() +dir +: +ƒ dir() +dirxml +: +ƒ dirxml() +error +: +ƒ error() +group +: +ƒ group() +groupCollapsed +: +ƒ groupCollapsed() +groupEnd +: +ƒ groupEnd() +info +: +ƒ info() +log +: +ƒ log() +memory +: +MemoryInfo {totalJSHeapSize: 10000000, usedJSHeapSize: 10000000, jsHeapSizeLimit: 2190000000} +profile +: +ƒ profile() +profileEnd +: +ƒ profileEnd() +table +: +ƒ table() +time +: +ƒ time() +timeEnd +: +ƒ timeEnd() +timeLog +: +ƒ timeLog() +timeStamp +: +ƒ timeStamp() +trace +: +ƒ trace() +warn +: +ƒ warn() Try also entering `typeof console` - +'object' Answer the following questions: What does `console` store? +It can not store the information it only display the information What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console is an object and log and assert are both functions '.' is used to access the functions like +console.log("hello world"); it display the message inside log on console +console.assert(condition,'message'); if condition is false then it display the message diff --git a/week-1/interpret/percentage-change.js b/week-1/interpret/percentage-change.js index 49b0ac15..7de1b434 100644 --- a/week-1/interpret/percentage-change.js +++ b/week-1/interpret/percentage-change.js @@ -12,9 +12,22 @@ 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 +// There are three function calls +// 1) carPrice.replaceAll(); +// 2) priceAfterOneYear.replaceAll() +// 3) console.log() // b) Identify all the lines that are variable reassignment statements +// There are two lines of Variable reassignment +// 1) carPrice = Number(carPrice.replaceAll(",", "")); After removing commas +// 2) priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); After removing commas // c) Identify all the lines that are variable declarations +// There are four lines of variable declaration +// 1) let carPrice +// 2) let priceAfterOneYear +// 3) const priceDifference +// 4) const percentageChange // d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +// It removes commas to the price and also convert the string into Numbers \ No newline at end of file diff --git a/week-1/interpret/time-format.js b/week-1/interpret/time-format.js index 7961fe0d..147e2e11 100644 --- a/week-1/interpret/time-format.js +++ b/week-1/interpret/time-format.js @@ -1,4 +1,4 @@ -const movieLength = 8784; // length of movie in seconds +const movieLength = 86400; // length of movie in seconds const remainingSeconds = movieLength % 60; const totalMinutes = (movieLength - remainingSeconds) / 60; @@ -14,16 +14,34 @@ 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? +// 1) const movieLength +// 2) const remainingSeconds +// 3) const remainingMinutes +// 4) const totalHours +// 5) const remainingHours +// 6) const result // b) How many function calls are there? +// console.log(result); // c) Using documentation on MDN, explain what the expression movieLength % 60 represents +// The operator % is modulus operator that gives reminder result It divide the movieLength value with 60 // d) Interpret line 4, what does the expression assigned to totalMinutes mean? +// const totalMinutes = (movieLength - remainingSeconds) / 60; +// movieLength - remainingSeconds: Removes the extra seconds that aren't part of a full minute from the total seconds. +// Converts the remaining time (in seconds) into minutes by dividing the total seconds by 60. // e) What do you think the variable result represents? Can you think of a better name for this variable? +// I think movieTotalTime is better because this program is about movie time so its better to assign variable name clearly // f) Think about whether this program will work for all values of movieLength. +// yes this code is work for all values in second only 24hours formate shows true result + // Think of what values may cause problems for the code. +// I enter 86400 value that is not work properly + // Decide the result should be for those values, then test it out. +// the result is 0:0:0 on entering 86400 // Can you find any values of movieLength which don't give you what you'd expect? +// yes the value that is greater then or equal to 86400 then code did not work properly \ No newline at end of file diff --git a/week-1/interpret/to-pounds.js b/week-1/interpret/to-pounds.js index 196be3b2..087f2d97 100644 --- a/week-1/interpret/to-pounds.js +++ b/week-1/interpret/to-pounds.js @@ -1,19 +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(paddedPenceNumberString.length - 2).padEnd(2, "0"); console.log(`£${pounds}.${pence}`);