Nice use of comments to give references. Very nice touch
2-more.js: While the result is correct, your function is not pure when it could easily be. You should be returning a string rather than logging it directly. Something like this would be closer to what was asked, and be better style.
function colorCar(color) {
return 'a ' + color + ' car';
}
console.log(colorCar('red'));
3-more.js: What would happen if I passed an object that looked like this to your function?
You function would break because the fields are hard-coded. With a title like printObject I should be able to pass any object and have the results be correct.
You want to iterate through all the fields and values of the object and print them out. Here is an example of one way to do this (there are many):
function printObject(obj) {
for (key in obj) {
console.log(key + ' = ' + obj[key]);
}
}
This will print out the dog object and the person object with no problem.
4-more.js:
Again, you should be returning strings from the function and then printing the return value from the function.
Other than that it is correct. However they mention that you should test with more than one value, and as you can see from the 3-more.js this is really important, as it would have uncovered that your function could only print one type of object.
5-more.js: Nice job. The ternary operator is your friend 😉
6-more.js:
Nice use of a ternary operator and a variable to handle whether the vehicle is 'new' or 'used'.
again, more test cases, you have only tested for an old car, you have not tested for a new car. So you know in the future you should have a test for each branch in your code. So, you would test these inputs at the very least:
vehicleType('red', 1, 1); // => 'a red new car'
vehicleType('blue', 1, 5); // => 'a blue used car'
vehicleType('green', 2, 1); // => 'a green new motorbike'
vehicleType('yellow', 2, 10); // => 'a yellow used motorbike'
vehicleType('magenta', 5, 1); // => 'unknown vehicle'
The code works, and the outputs are correct. Well done.
7-more.js:
What happens if someone adds a new type to the vehicle array? e.g. 'truck'?
Again, it specifically says to return a string, not print directly to console.log.
Again too few tests of the code. You should have a test for each type of vehicle, and its color and condition.
You have the right idea because you use the array to get the vehicle value, but I would be inclined to save myself a lot of typing by just passing the code directly to the array (maybe with one less). Here is an example:
function vehicleType(color, code, age){
const vehicle = ['car', 'motorbike', 'caravan', 'bike'];
const condition = age <= 1 ? ' new ' : ' used ';
if(code > vehicle.length){
return 'unknown vehicle'
} else {
return 'a ' + color + condition + vehicle[code - 1];
}
}
vehicleType('green', 3, 1);
8-more.js:
Did you run this code? Because it is not to spec. i.e. you are missing the ' and ' before the last element?
in the advertisement function I believe you should be referencing 'vehicleNames' rather than 'category'. 'Category' is not in the function, and is being accessed when it is called because you defined it just before calling the function. This makes your function impure, and will lead to bugs when you you use your code in different contexts.
Well done to return a string, and then console.log the returned string from the function call rather than using console.log within the function itself.
You really needed to iterate over the array (see code samples above) and when you reached the last element add the ' and ', the value and a '.' to end the sentence.
9-more.js
While you adjusted your function to handle the 'scooter' addition. The idea was to add the element and see if they function worked without changing anything else. Refer to my code above for just using a reference to the array index to handle the situation.
10-more.js
Looks perfect. Nice job.
11-more.js
Again, nice job. Looks perfect.
12-more.js
Nice. o3 is a reference to o2. So, the values inside the object are shared between o2 and o3.
13-more.js
42 is not a string datatype, it is a number (an integer specifically).
But your second comment is correct and gives the correct answer: typeof returns a string. It does not matter what the first type is, because typeof always returns a string describing the type of the value.
Hi Dumie,
I'm sorry about the formatting for the functions. Apparently the "code" mode ignores new lines, which is nuts.
I read the markdown docs, seems I needed a different technique. Should look better now.
Hi Dumie, Some feedback on your week3 homework.
Nice use of comments to give references. Very nice touch
2-more.js: While the result is correct, your function is not pure when it could easily be. You should be returning a string rather than logging it directly. Something like this would be closer to what was asked, and be better style.
3-more.js: What would happen if I passed an object that looked like this to your function?
You function would break because the fields are hard-coded. With a title like printObject I should be able to pass any object and have the results be correct.
You want to iterate through all the fields and values of the object and print them out. Here is an example of one way to do this (there are many):
This will print out the dog object and the person object with no problem.
4-more.js:
5-more.js: Nice job. The ternary operator is your friend 😉
6-more.js:
7-more.js:
8-more.js:
9-more.js
10-more.js
11-more.js
12-more.js
13-more.js