London | 26-ITP-Sep | Sakiya Mayow | Sprint 2 | JavaScript-Fundamentals - #1573
zakiaao-tech wants to merge 19 commits into
Conversation
✅ Deploy Preview for cyf-onboarding-module ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Thanks for rebuilding the branch. It is clean now, with only your 14 Sprint 2 commits.
Your explanations are detailed. In 4-random.js you work through the expression in the right order.
There is one big problem first. Most of your answers are written as plain text in the .js files. Node reads every line of a .js file as code. Your answers are sentences, not code. So 10 of your 12 files stop with a SyntaxError. Only 2-initials.js and 3-paths.js run.
You already wrote the fix in your answer in 0.js. Two slashes // turn a line into a comment. Node ignores comments, so it does not try to run them.
The rule: in a .js file, every line of an answer must start with //.
After that, run each file with node to check it works.
Things to fix before I can mark this Complete:
-
Put
//at the start of every answer line, in every.jsfile. See my comment on1-count.js. -
2-mandatory-errors/0.js: lines 1 and 2 still run. See my comment. -
2-mandatory-errors/2.js: lines 6 and 7 still run. See my comment. -
3-mandatory-interpret/1-percentage-change.jsline 5: the comma is still missing, so the file cannot run. -
1-key-exercises/3-paths.js:dirandextare not right yet. See my comments. -
1-key-exercises/4-random.jsline 26. See my comment. -
2-mandatory-errors/1.js,2.jsand4.js: add the name of the error. The prep shows three error names: SyntaxError, TypeError and ReferenceError. Which one was each? -
2-mandatory-errors/3.js: the fix changes the wrong line. See my comment. -
3-mandatory-interpret/2-time-format.js: answers c) and f), and e) still needs a better name forresult. -
2-initials.jsline 8: delete the old starter line. Git keeps it for you. -
Formatting. "My changes follow the style guide" is on your checklist, and consistent formatting is part of it. The tool that does it is called Prettier. It cannot format a file with a SyntaxError, so do this after step 1.
Prettier comes with the CYF extension pack from onboarding. If you are not sure you have it, open VS Code, go to Extensions, and search for CodeYourFuture Extension Pack: https://marketplace.visualstudio.com/items?itemName=CodeYourFuture.cyf-extension-pack
Then open each file you changed, right click in the editor, and choose Format Document. Pick Prettier if VS Code asks. Save, commit and push. To format every time you save, follow the format on save steps here: https://github.com/CodeYourFuture/Module-JavaScript-Fundamentals/blob/main/practical_guide.md
Add the Needs Review label again once you have pushed.
|
|
||
|
|
||
|
|
||
| Line 1 is a declaration and Line 3 is a statement that changes the value of the variable 'count' by adding 1 to its current value. |
There was a problem hiding this comment.
Your answer is right. But run node 1-count.js. Node stops here with a SyntaxError. It tries to read your sentence as code.
Start each answer line with //. Then the line is a comment, and node skips it. Do this in every .js file.
There was a problem hiding this comment.
All your answers are comments now, and every file runs. Good.
| @@ -1,2 +1,6 @@ | |||
| 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 | |||
| We don't | |||
There was a problem hiding this comment.
Run node 0.js. Line 1 has no //, so node still reads it. Also, the original line 2 is now split over lines 2 and 3. Where does each // need to go?
There was a problem hiding this comment.
Both lines are comments now, and the file runs.
|
|
||
| error | ||
|
|
||
| console.log(`I was born in ${cityOfBirth}`); |
There was a problem hiding this comment.
Lines 6 and 7 are the original code, and they still run. So the file still stops with the same error. Your fixed version on lines 16 and 17 is right. What should happen to lines 6 and 7?
There was a problem hiding this comment.
The file runs now, and your error / right way layout is clear. Good.
|
|
||
| const dir = ; | ||
| const ext = ; | ||
| const dir = filePath.slice(1, lastSlashIndex); |
There was a problem hiding this comment.
Log dir. It prints Users/mitch/.... The path starts with a /. In the diagram, the root / is part of dir. Which index does your slice start at?
| const ext = ; | ||
| const dir = filePath.slice(1, lastSlashIndex); | ||
|
|
||
| const ext = base.slice(45); |
There was a problem hiding this comment.
Log ext. What does it print? base is "file.txt", only 8 characters. What is at index 45? Line 13 finds the last /. Could you find the . the same way?
There was a problem hiding this comment.
ext prints .txt now, and it works for any file name. Good.
| Next, we need to multiply the result from Math.random() by 100. This may give us the result of a decimal between 0 and 100. | ||
| Next step is to use Math.floor() wich then rounds down the decimal number to the nearest whole integer. This then means that the result will be a whole number between 0 and 99. | ||
| But we want 1-100, so we add 1 to the result of Math.floor() this then changes the range to be inclusive of 1 and 100. | ||
| After adding 1, we need to round down the number to the nearest interger, this is done by using Math.floor(). After this we will have a whole number between maximum and minimum. |
There was a problem hiding this comment.
Look at line 4 again. How many times is Math.floor used? Does it happen before or after + minimum?
There was a problem hiding this comment.
Line 21 is right. But line 29 still says you round down after adding 1. That is the opposite order. Delete line 29.
There was a problem hiding this comment.
Line 29 is gone, and the order is right now.
|
|
||
| When run with console.log(last4Digits); we get --> TypeError : cardNumber.slice is not a function | ||
|
|
||
| .slice is a method used with strings and we need to make cardNumber into a string by adding quotation marks.I |
There was a problem hiding this comment.
Your prediction and the error message are good. But you changed cardNumber on line 1 into a string. The exercise asks you to change the expression on line 2 instead. How can line 2 turn the number into a string?
There was a problem hiding this comment.
Line 21 is exactly right. Just check it says cardNumber, with a capital N.
You pass cardNumber to the function String. It converts the number into a string. You explained this in your comment. But in your code, you made it a string by hand. You put quotes around the number on line 1.
Please update your code to use String. That is what it is designed to do. Put line 1 back to the number, then change line 2.
There was a problem hiding this comment.
Line 2 uses String now, and line 1 is a number again. Your code is right, and line 21 explains it well.
Some old notes still describe your first fix. Line 19 ends with "by adding quotation marks". Lines 23 and 24 say you put quotes around the number on line 1. You don't do that any more. Please change the end of line 19 to match your new fix, and delete lines 23 and 24.
| There are 5 function calls, two function calls on line 4 = Number(...) and replaceAll(...) two more on line 5 = Number(...) and replaceAll(...) and finally on line 10 = 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? | ||
|
|
||
| SyntaxError: missing a comma to separate the arguments |
There was a problem hiding this comment.
Right reason. Which line is the error on? The comma is still missing on line 5, too.
There was a problem hiding this comment.
The comma is fixed and the file runs. The question also asks which line the error was on. Add the line number.
There was a problem hiding this comment.
Line 5 is right. Done.
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
| movieLength % 60 means finding the remainder after dividing movieLength by 60. movieLength = 8784 so we need to do: 8784 % 60 = 146.4 and the nearest whole integer is 146. |
There was a problem hiding this comment.
Is 146.4 what 8784 % 60 gives? Or is that 8784 / 60? Try both in node and compare.
There was a problem hiding this comment.
Line 25 is right: 8784 % 60 is 24. But line 23 says 8784 % 60 = 146.4. Did you mean / instead of % on line 23?
| The result variable will show us he complete results from our calculations of = totalHours:remainingMinutes:remainingSeconds. | ||
| result = 2:26:24 | ||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| I tested differnet codes: 7965 and I got the result 2:12:45 and i also tried : 2654 and got 0:44:14. No newline at end of file |
There was a problem hiding this comment.
Both of those work. Now try 59, -90 and 90.5. What does each one print? Does the code work for all values?
There was a problem hiding this comment.
Good. 59, -90 and 90.5 all answered.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
That is most of the list done, thanks. Every file runs now, and 3-paths.js gives the right dir and ext.
Still to do before I can mark this Complete:
2-mandatory-errors/1.js,2.jsand4.js: the error type is still missing. See my comment on each file.2-mandatory-errors/3.js: the fix is still on line 1. Your line 21 says the right thing. See my comment.4-random.jsline 29 and2-time-format.jsline 23. See my comments.2-time-format.jsanswer e): a better name for the variableresultis still missing.1-percentage-change.jsanswer b): which line was the error on?- Prettier again. All 14 files still fail. Format each file you changed, then push. Format on save does it for you every time: https://github.com/CodeYourFuture/Module-JavaScript-Fundamentals/blob/main/practical_guide.md
Add the Needs Review label again once you have pushed.
|
|
||
|
|
||
|
|
||
| //const means that the variable age is a constant and cannot be reassigned. When we try to reassign the value of age by adding 1 to it, we get an error. |
There was a problem hiding this comment.
Good reasoning. You explain why const cannot be reassigned, and why let fixes it.
You also need to name the error type. JavaScript has different error types. The prep covered three: TypeError, ReferenceError and SyntaxError. The error type will be obvious from the error itself. Which one is this? Please add it to your explanation of the error.
There was a problem hiding this comment.
The error type is TypeError. It is the first word of the error message node showed when you ran the original file.
Please add this line above your explanation on line 9:
// The error is a TypeError: Assignment to constant variable.| //console.log(`I was born in ${cityOfBirth}`); | ||
| //const cityOfBirth = "Bolton"; | ||
|
|
||
| //The reason there is an error is because we are using the wrong order, we are telling Javascript to print the value of cityOfBirth before creating a variable. |
There was a problem hiding this comment.
Good reasoning. You explain that JavaScript runs from top to bottom, so the variable must be declared first.
You also need to name the error type. JavaScript has different error types. The prep covered three: TypeError, ReferenceError and SyntaxError. The error type will be obvious from the error itself. Which one is this? Please add it to your explanation of the error.
There was a problem hiding this comment.
The error type is ReferenceError. It is the first word of the error message node showed when you ran the original file.
Please add this line above your explanation on line 9:
// The error is a ReferenceError: Cannot access 'cityOfBirth' before initialization|
|
||
|
|
||
|
|
||
| //Variable names cannot start with a number as Javascript doesn't allow it, it can contain a number inside the variable but not at the beginning. |
There was a problem hiding this comment.
Good reasoning. A variable name cannot start with a number.
You also need to name the error type. JavaScript has different error types. The prep covered three: TypeError, ReferenceError and SyntaxError. The error type will be obvious from the error itself. Which one is this? Please add it to your explanation of the error.
There was a problem hiding this comment.
The error type is SyntaxError. It is the first word of the error message node showed when you ran the original file.
Please add this line above your explanation on line 12:
// The error is a SyntaxError: Invalid or unexpected token| //we then subtract remainingSeconds from movieLength= 8784 -24 = 8670. we then have to divide it by 60 which gives us 8760 / 60 = 146. we divide because there is 60 seconds in one minute, and right now we are converting seconds into minutes. | ||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
| //>The result variable will show us he complete results from our calculations of = totalHours:remainingMinutes:remainingSeconds. |
There was a problem hiding this comment.
Your answer to the first part of e) is right. The question also asks for a better name for the variable result.
The name result is vague. Any calculation has a result. So the name does not tell us what this value is.
Can you suggest a better name? Pick one that explains what this value is.
There was a problem hiding this comment.
A good name says what the value is. result holds the movie length, written as hours:minutes:seconds. So a name like movieDuration or formattedTime works. Pick one of these, or your own, and add it to e).
abdishakoor-dev
left a comment
There was a problem hiding this comment.
Nearly done. 3.js now uses String on line 2, and your answer to b) names line 5.
Still to do before I can mark this Complete:
1.js,2.jsand4.js: the error type is still missing. My reply on each file has the line to add.2-time-format.jse): a better name forresult. See my reply.3.js: your code is right. But some old notes describe your first fix. Please update the end of line 19, and delete lines 23 and 24.- Prettier: all 14 files still fail. Open each file you changed, right click, and choose Format Document. If Prettier is not in the list, install the CYF extension pack first: https://marketplace.visualstudio.com/items?itemName=CodeYourFuture.cyf-extension-pack
To format every time you save, follow these steps: https://github.com/CodeYourFuture/Module-JavaScript-Fundamentals/blob/main/practical_guide.md
Add the Needs Review label again once you have pushed.
abdishakoor-dev
left a comment
There was a problem hiding this comment.
All fixed. Every file runs, the error types are in, and Prettier passes on all your files now. Marking this Complete. Well done.
A few small things for your next PRs. You don't need to change them here:
- Variable names in JavaScript use camelCase. Each new word starts with a capital letter, so
movieTime, notmovietime(2-time-format.jsline 34). - Delete old code instead of leaving it commented out, like
//console.log(result);on line 35 of2-time-format.js. Git keeps the old version for you. - Check that the explanations in your comments match your code.
3.jslines 23 and 24 saycardNumberbecomes a string. ButcardNumberis still a number.String(cardNumber)makes a new string from it, and.sliceuses that.

Learners, PR Template
Self checklist
Task code
CYF-1039
Changelist
I completed the 4 exercises for my coursework, and provided answers where needed. These exercises included slice() methods, functions, index, number, variables, mathematical calculations, correcting errors, dividing, and objects.