Homework JavaScript 1
wael-alhomsi / hyf-javascript1 Goto Github PK
View Code? Open in Web Editor NEWHomework JavaScript 1
Homework JavaScript 1
Hi Wael, here is my feedback on your homework for week 2.
Overall you have done a good job. The code works as expected. Your choice of variable names is excellent.
1. It is best to define your variables just before you need them. This keeps related code together. In this tiny program it is not urgent, but the principle remains:
const re = /,/gi;
console.log(myString.replace(re, ' '));
Both variables can be declared as const
. You should use const
instead of let
if you do not intend to reassign the variable somewhere else in your code.
2. This file has formatting issues. Please check the VSCode Tips on how to set up VSCode to automatically format your code.
3. Be consistent in your use of quotes as string delimiter. Either choose single quotes or double quotes and then stick to that for all of your code (I prefer single quotes myself).
4. Rather than using the expression favoriteAnimals.indexOf('meerkat')
twice, you could define a variable and then use that:
const index = favoriteAnimals.indexOf('meerkat');
5. This file has formatting issues.
6. For better readability it would be nice to separate blocks of related code with a blank line, just as you would separate paragraphs in a piece of text with a blank line:
const months = [
//...
];
let totalDays = 0;
for (let i = 0; i < months.length; i++) {
//...
}
if (totalDays === 365) {
//...
} else {
//...
}
7. This file has formatting issues.
8. You can use const
instead of let
for those variables that do not (need to) change. This will also remove the green wavy underlines (=warning) from ESLint.
Hi Wael, here is my feedback on your homework for week 3.
Correct!
But by calling your function invoiceAmount()
you are suggesting that this function can only be used for that purpose, while in fact it can add up any three numbers. The more generic name sum()
would invite wider usage.
Also, make sure that your function name suggests an action, which is usually done by embedding a verb in the name. If your function is specifically about computing invoice amounts you could call it for instance computeInvoiceAmount()
.
Note that the let invoice
could have been const invoice
, as indicated by ESLint. In general, you should follow up on all issues identified by ESLint. That's the whole point of ESLint: to give you warnings and errors where your code can (and should) be improved.
Correct!
Correct!
(There is a missing semicolon in line 4, as flagged by ESLint).
Correct!
Correct!
Correct, but you can simplify your code by dealing with the age separately:
function vehicleType(color, code, age) {
const condition = age <= 1 ? 'new' : 'used';
if (code === 1) {
return 'a ' + color + ' ' + condition + ' car';
} else if (code === 2) {
return 'a ' + color + ' ' + condition + ' motorbike';
} else if (code > 2) {
return 'a ' + color + ' ' + condition + ' unknown vehicle';
}
}
Your code produces the correct results but is overly complicated. It is best to deal with color
, code
and age
individually and then compose a result through a concatenation of the individual results, like this:
function vehicleType(color, code, age) {
const arrVehicleTypes = ['car', 'motorbike', 'caravan', 'bike'];
const condition = age <= 1 ? 'new' : 'used';
const vehicle = (code >= 1 && code <= arrVehicleTypes.length)
? arrVehicleTypes[code - 1]
: 'unknown vehicle';
return 'a ' + color + ' ' + condition + ' ' + vehicle;
}
Also, notice that I placed the vehicles array inside the function body so that the function no longer depends on an external variable and is now self-contained: it has become pure: the most desirable form for a function.
Correct. But mind the code formatting. You should never add code on the same line after a closing curly brace:
// bad
... } return 'Amazing Wael\'s Garage, we service ' + str + ' and ' + arr[arr.length - 1] + 's.';
// good
... }
return 'Amazing Wael\'s Garage, we service ' + str + ' and ' + arr[arr.length - 1] + 's.';
See Code Formatting.
Correct! (Mind code formatting)
Correct!
To use a metaphor: x
and y
are like twin brothers, they look the same but are separate entities. However, z
is just another name for x
.
Correct!
Correct!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.