London | 26-ITP-Sep | Sakiya Mayow | Sprint 2 | JavaScript-Fundamentals - #1573
zakiaao-tech wants to merge 17 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.
| @@ -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?
|
|
||
| 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?
|
|
||
| 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?
| 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?
|
|
||
| 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 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.
| // 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.
| 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?

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.