diff --git a/1-key-exercises/1-count.js b/1-key-exercises/1-count.js new file mode 100644 index 000000000..eeb9e9673 --- /dev/null +++ b/1-key-exercises/1-count.js @@ -0,0 +1,7 @@ +let count = 0; + +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 = reassign a variable using the = + operator. +//Line 1 assigns count = 0. Line 3 uses that value (0) and adds 1, so count becomes 1. \ No newline at end of file diff --git a/1-key-exercises/2-initials.js b/1-key-exercises/2-initials.js new file mode 100644 index 000000000..5319d5d12 --- /dev/null +++ b/1-key-exercises/2-initials.js @@ -0,0 +1,9 @@ +const firstName = "Creola"; +const middleName = "Katherine"; +const 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. + +const initials = `${firstName.charAt(0)}${middleName.charAt(0)}${lastName.charAt(0)}`; +console.log(initials) +// // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/1-key-exercises/3-paths.js b/1-key-exercises/3-paths.js new file mode 100644 index 000000000..575259d63 --- /dev/null +++ b/1-key-exercises/3-paths.js @@ -0,0 +1,25 @@ +// The diagram below shows the different names for parts of a file path on a Unix operating system + +// ┌─────────────────────┬────────────┐ +// │ dir │ base │ +// ├──────┬ ├──────┬─────┤ +// │ root │ │ name │ ext │ +// " / home/user/dir / file .txt " +// └──────┴──────────────┴──────┴─────┘ + +// (All spaces in the "" line should be ignored. They are purely for formatting.) + +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}`); + +// 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 = filePath.slice(0, lastSlashIndex); +const ext = base.slice(base.lastIndexOf(".")); + +// https://www.google.com/search?q=slice+mdn +console.log(`The dir part of ${filePath} is ${dir}`); +console.log(`The ext part of ${filePath} is ${ext}`); \ No newline at end of file diff --git a/1-key-exercises/4-random.js b/1-key-exercises/4-random.js new file mode 100644 index 000000000..8ddf8390b --- /dev/null +++ b/1-key-exercises/4-random.js @@ -0,0 +1,18 @@ +const minimum = 1; +const maximum = 100; + +const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; + +// 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 + +// In this exercise, num represent the value integer number between 1 to 100. +//Math.random generates a random decimal number from 0 up to 1, but not including, 1. +// down to the nearest whole number (integer). +// i think Changes the whole range to be greater by the minimum value +//Ensures the value never comes less than 1. +//Running the program several times generate the whole number(integer) like (1,10,15,44,66,55) several times between 1 to 100 all 100 number has a equal 1% chance to appear(generate). +console . log (num) +// \ No newline at end of file diff --git a/2-mandatory-errors/0.js b/2-mandatory-errors/0.js new file mode 100644 index 000000000..520a434a5 --- /dev/null +++ b/2-mandatory-errors/0.js @@ -0,0 +1,3 @@ +/*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 should we solve this problem? +for single line we used // and for multiple line use */ \ No newline at end of file diff --git a/2-mandatory-errors/1.js b/2-mandatory-errors/1.js new file mode 100644 index 000000000..875eee972 --- /dev/null +++ b/2-mandatory-errors/1.js @@ -0,0 +1,11 @@ +// trying to create an age variable and then reassign the value by 1 + +let age = 33; +age = age + 1; + +console.log(age); + +/* In this case age is not const means variable is not reassigned so that, +/*we throws a TypeError: Assignment to constant variable*/ + //I try by let where console.log(age) shows Running] node "c:\Users\Desktop\code your future\Module-Onboarding\Module-JavaScript-Fundamentals\Sprint-2\Sprint-2\2-mandatory-errors\1.js" +//34. diff --git a/2-mandatory-errors/2.js b/2-mandatory-errors/2.js new file mode 100644 index 000000000..43d5d8516 --- /dev/null +++ b/2-mandatory-errors/2.js @@ -0,0 +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}`); +const cityOfBirth = "Bolton"; + + +/*we need to put declare variables(const cityOfBirth = "Bolton";) in first line after using expression console.log. \ No newline at end of file diff --git a/2-mandatory-errors/3.js b/2-mandatory-errors/3.js new file mode 100644 index 000000000..affb713d4 --- /dev/null +++ b/2-mandatory-errors/3.js @@ -0,0 +1,14 @@ +const cardNumber = 4533787178994213; +const last4Digits = String(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 +//Card number is a number not a string so it will throw typeerror. +// i did not think slice method can not run in number method so that i change it in string +// when i put capital letter ReferenceError: string is not defined so that nothing last4Digits not showed +//in the terminal String is the function but string is the normal text. \ No newline at end of file diff --git a/2-mandatory-errors/4.js b/2-mandatory-errors/4.js new file mode 100644 index 000000000..67705bc68 --- /dev/null +++ b/2-mandatory-errors/4.js @@ -0,0 +1,3 @@ +const 12HourClockTime = "8:53pm"; +const 24hourClockTime = "20:53"; +/*variable is not start with number show syntaxerror. diff --git a/3-mandatory-interpret/1-percentage-change.js b/3-mandatory-interpret/1-percentage-change.js new file mode 100644 index 000000000..458fa6d7f --- /dev/null +++ b/3-mandatory-interpret/1-percentage-change.js @@ -0,0 +1,35 @@ +let carPrice = "10,000"; +let priceAfterOneYear = "8,543"; + +carPrice = Number(carPrice.replaceAll(",", "")); +priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",","")); + +const priceDifference = carPrice - priceAfterOneYear; +const percentageChange = (priceDifference / carPrice) * 100; + +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 has five function in line 4 and and console . log also has one +line 4 and 5 has two Number() function +line 4 and 5 has two replaceALL() function +line 10 has one console . 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? + In line five error shown in replaceAll("," "")); missing comma before last two comma(",",""). + +/* c) Identify all the lines that are variable reassignment statements +line 4 carPrice = Number(carPrice.replaceAll(",", "")); +line 5 priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); +As per the carPrice and priceAfterOneYear has been declared in line 1 and 2 by using let variable. + +/*d) Identify all the lines that are variable declarations +line 1 let carPrice = "10,000"; +line 2 let priceAfterOneYear = "8,543"; +line 7 const priceDifference = carPrice - priceAfterOneYear; +line 8 const percentageChange = (priceDifference / carPrice) * 100; + +/* e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? +replaceALL throwout all the commas from the string and number became numerical value. diff --git a/3-mandatory-interpret/2-time-format.js b/3-mandatory-interpret/2-time-format.js new file mode 100644 index 000000000..72f0689d4 --- /dev/null +++ b/3-mandatory-interpret/2-time-format.js @@ -0,0 +1,34 @@ +const movieLength = 8784; +const remainingSeconds = movieLength % 60; +const totalMinutes = (movieLength - remainingSeconds) / 60; + +const remainingMinutes = totalMinutes % 60; +const totalHours = (totalMinutes - remainingMinutes) / 60; + +const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; +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? +/* line 1 const movieLength line 3 const remainingSeconds line 4 const totalMinutes + line 6 const remainingMinutes line 7 const totalHours line 9 const result + +// b) How many function calls are there? +/* there are one function + console.log(result); + +// c) Using documentation, explain what the expression movieLength % 60 represents +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators +/*The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend. + +// d) Interpret line 4, what does the expression assigned to totalMinutes mean? +/* This expression convert movies time second into minutes dividend by 60 second. + +// e) What do you think the variable result represents? Can you think of a better name for this variable? +/* The variable represent the movieLength in second,minutes and hours. + We can change this name as movieTime. + +// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer +/* In this section we can only use the positive natural numbers but if we put numbers that divide by 60 without a remainder get the exact time like 10, 20 50. diff --git a/3-mandatory-interpret/3-to-pounds.js b/3-mandatory-interpret/3-to-pounds.js new file mode 100644 index 000000000..59d3eb0f1 --- /dev/null +++ b/3-mandatory-interpret/3-to-pounds.js @@ -0,0 +1,32 @@ +const penceString = "399p"; + +const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 +); +// remove the p from the "399p" and make "399" +const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); +//padStart() add characters to the starting of the string and provide 3 long characters. +//Because we have to convert pound and pence. +const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 +); +// we take out the 2 character and store the remaining part of the string as the pound. + +const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + // At last we get the last two characters as the pence. + +console.log(`£${pounds}.${pence}`); +//We can display the price of pound and pence like £3.99. + +// This program takes a string representing a price in pence +// The program then builds up a string representing the price in pounds + +// 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" diff --git a/4-stretch-explore/chrome.md b/4-stretch-explore/chrome.md new file mode 100644 index 000000000..27494e68a --- /dev/null +++ b/4-stretch-explore/chrome.md @@ -0,0 +1,19 @@ +Open a new window in Chrome, right click an empty space on the page, select **Inspect** from the dropdown, then locate the **Console** tab. + +Voila! You now have access to the [Chrome V8 Engine](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-chrome-v8/). +Just like the Node REPL, you can input JavaScript code into the Console tab and the V8 engine will execute it. + +Let's try an example. + +In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`; + +What effect does calling the `alert` function have? +Answer An alert popped up + +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`. + +What effect does calling the `prompt` function have? +Answer A popup prompts me to write my name. + +What is the return value of `prompt`? +Answer The value i entered in the text box. diff --git a/4-stretch-explore/objects.md b/4-stretch-explore/objects.md new file mode 100644 index 000000000..ccad49517 --- /dev/null +++ b/4-stretch-explore/objects.md @@ -0,0 +1,23 @@ +## Objects + +In this activity, we'll explore some additional concepts that you'll encounter in more depth later on in the course. + +Open the Chrome devtools Console, type in `console.log` and then hit enter + +What output do you get? +Answer The output is ƒ log() { [native code] } + +Now enter just `console` in the Console, what output do you get back? +Answer console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, ...} but i can't copy it. + + +Try also entering `typeof console` +Answer It shows object' + +Answer the following questions: + +What does `console` store? +console is an object with properties that are function values. These functions are called methods because they belong to the console object. + +What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? +console.log means "access the log method from the console object" and console.assert means "access the assert method from the console object. diff --git a/README.md b/README.md new file mode 100644 index 000000000..a47afc540 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# 🧭 Guide to Sprint 2 exercises + +> https://curriculum.codeyourfuture.io/itp/javascript-fundamentals/sprints/2/prep/ + +> [!TIP] +> You should always do the prep work _before_ attempting the coursework. +> The prep shows you _how_ to do the coursework. +> There is often a step by step video you can code along with too. +> Do the prep. + +This README will guide you through the different sections for this week. + +## 1 Exercises + +In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 2 Errors + +In this section, you'll need to go to each file in `errors` directory and run the file with node to check what the error is. Your task is to interpret the error message and explain why it occurs. The [errors documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors) will help you figure out the solution. + +## 3 Interpret + +In these tasks, you have to interpret a slightly larger program with some syntax / operators / functions that may be unfamiliar. + +You must use documentation to make sense of anything unfamiliar - learning how to look things up this way is a fundamental part of being a developer! + +You can also use `console.log` to check the value of different variables in the code. + +https://developer.mozilla.org/en-US/docs/Web/JavaScript + +## 4 Explore - Stretch 💪 + +This stretch activity will get you to start exploring new concepts and environments by yourself. It will do so by prompting you to reflect on some questions.