-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NW6 |Zeliha Pala | JS1 Module | [TECH ED] Complete week 1 exercises | Week1 #133
base: main
Are you sure you want to change the base?
Changes from 1 commit
7ef556f
e6a23a9
d8034b5
94abab1
609ca15
a3530e9
a736fb1
16fe0b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,26 @@ | |
|
||
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 | ||
Open the Chrome devtools Console, type in `console.log` and then hit enter | ||
ƒ log() { [native code] } | ||
|
||
What output do you get? | ||
|
||
Now enter just `console` in the Console, what output do you get back? | ||
|
||
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} | ||
|
||
Try also entering `typeof console` | ||
|
||
typeof console | ||
'object' | ||
|
||
Answer the following questions: | ||
|
||
What does `console` store? | ||
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? | ||
What does the syntax `console.log` or `console.assert` mean? | ||
In JavaScript, the console is like a toolbox for developers. It helps them see messages, warnings, and errors while building and fixing things on a website. It's a way to peek behind the scenes and understand what's happening in their code. console.log is like a sticky note to jot down messages for themselves, and console.assert is a tool to shout out if something isn't right. | ||
|
||
In particular, what does the `.` mean? | ||
|
||
The . (dot) in JavaScript is called the dot notation and is used to access properties and methods of objects. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, in the case of console.log and console.assert, what is being accessed? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
let carPrice = "10,000"; | ||
let priceAfterOneYear = "8,543"; | ||
|
||
carPrice = Number(carPrice.replaceAll(",", "")); | ||
carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato | ||
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
|
||
const priceDifference = carPrice - priceAfterOneYear; | ||
|
@@ -12,9 +12,15 @@ 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 | ||
//Number is a function. replaceAll is a method | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one. So how many function calls would you say are in this file? |
||
|
||
// b) Identify all the lines that are variable reassignment statements | ||
//carPrice = Number(carPrice.replaceAll(",", "")); // converts to number takes the string quato | ||
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", "")); | ||
//the values of the variables carPrice and priceAfterOneYear has bee changed with removing commas and converting them to numbers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good explaination 👍 |
||
|
||
// c) Identify all the lines that are variable declarations | ||
//let keyword is used to declare two variables, carPrice and priceAfterOneYear, and initialize them with string values. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any other keywords used in this code that could also declare variables? |
||
|
||
// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? | ||
//replaceAll turns into an empty character with .replaceAll, and it turns into a number with number |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,40 @@ 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? | ||
// a) How many variable declarations are there in this program? 6 | ||
/* | ||
const movieLength = 8784; | ||
const remainingSeconds = movieLength % 60; | ||
const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
const remainingMinutes = totalMinutes % 60; | ||
const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
const remainingHours = totalHours % 24; | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nearly! It looks like you're missing 1 here 👀 |
||
|
||
// b) How many function calls are there? | ||
// b) How many function calls are there? 1 | ||
//console.log(result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent spot! ⭐ |
||
|
||
// c) Using documentation on MDN, explain what the expression movieLength % 60 represents | ||
//This is modulo operation, and it returns the remainder of the division. Here it says the variable movieLength, is divided by 60 and The % modulo operator, returns the remainder of the division operation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👌 |
||
|
||
// d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
||
/*to find the total minutes of the movie; | ||
we Subtract the remaining seconds (remainingSeconds) from the total seconds (movieLength) | ||
and we Take the result and divide it by 60. | ||
The final result is the total number of minutes in the movie, stored in the variable totalMinutes."*/ | ||
|
||
// e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
/* It expresses the length of the movie in terms of hours, minutes, and seconds. | ||
The format is HH:MM:SS, where HH is the remaining hours, MM is the remaining minutes, and SS is the remaining seconds. | ||
My suggestion for result variable is : | ||
const movieDurationDisplay */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice 👍 |
||
|
||
// f) Think about whether this program will work for all values of movieLength. | ||
//The program correctly converts the total seconds into a formatted string representing hours, minutes, and seconds.It should work correctly for any positive integer value of movieLength. | ||
|
||
// Think of what values may cause problems for the code. | ||
|
||
// Decide the result should be for those values, then test it out. | ||
|
||
// Can you find any values of movieLength which don't give you what you'd expect? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've got the right answer above. Given that, what are some example values for movieLength that might cause problems for the code? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,19 @@ const penceString = "399p"; | |
|
||
const penceStringWithoutTrailingP = penceString.substring( | ||
0, | ||
penceString.length - 1 | ||
penceString.length - 1 // takes out the p with -1 | ||
); | ||
|
||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // padStarts add a zero infront of the number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. padStart will only add 0 in front in certain situations. What situations are that? In this case, how many 0s are added in front of the number? |
||
const pounds = paddedPenceNumberString.substring( | ||
0, | ||
paddedPenceNumberString.length - 2 | ||
paddedPenceNumberString.length - 2 // last two numbers ignored we have 03 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't quite right yet. What adds the 0? |
||
); | ||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) | ||
.padEnd(2, "0"); | ||
//This method is used to pad a string with a specified character at the beginning until it reaches a certain length. While doing this, the original string remains unchanged; a new string is returned.The first parameter of the padStart method specifies the target length, and the second parameter specifies the character to be added during the padding. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you've explained the padStart method. That's not used in this statement. Try to break this statement down to it components and explain what it's trying to do |
||
|
||
console.log(`£${pounds}.${pence}`); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well explained!