Giter Site home page Giter Site logo

hyf-javascript1's People

Contributors

amudar avatar

Watchers

 avatar

hyf-javascript1's Issues

Feedback homework week 3

Hi Mudar, here is my feedback on your homework for week 3.

1-more.js

Correct!

2-more.js

Correct!

But your second function modelCar has a side effect (console.log). In general, functions that are pure and free of side effects are preferred (like your first function).

3-more.js

Correct!

(But be consistent in your use of quotation marks: either all single or all double quotes).

Excellent that you made your function call itself recursively to handle embedded objects. I tried it like this:

const person = {
  firstName: 'Maartje',
  lastName: 'Kruijt',
  city: 'Amsterdam',
  accounts: {
    slack: '@maartjekruit',
    github: 'mkruit'
  }
};

You can further enhance this recursive function by adding indentation for embedded objects like this:

function printObject(obj, level = 0) {
  const indentation = ' '.repeat(level * 2);
  for (const prop in obj) {
    if (typeof obj[prop] === 'object') {
      console.log(indentation + prop + ':');
      printObject(obj[prop], level + 1);
    }
    else {
      console.log(indentation + prop + ': ' + obj[prop]);
    }
  }
}

With the object above, this gives the output:

firstName: Maartje
lastName: Kruijt
city: Amsterdam
accounts:
  slack: @maartjekruit
  github: mkruit

4-more.js

I think you have overlooked this from the assignment:

Your function should return a string, ...

Functions that are free of side effects (including console.log) are preferred. You can just return a string and do the console.log when you call the function.

5-more.js

Correct!

6-more.js

While this code produces correct results (but see the comment under 4), it has a number of coding and formatting issues.

  1. There is no point in assigning the value of color to result and then use result in the place of color.

  2. In general, you should not reassign parameters inside the function body. Moreover, if you reassign the parameter with the word new or used the name age does no longer reflects the content.

    You can use a ternary operator with a new variable that better reflects the content (see below).

  3. In the assignment it was stated that any car above one year old should be considered used. You increased that to two years, which deviates from what was asked.

  4. Your else clause in line 9 should not be on a single line.

  5. I would leave out the blank lines 14 and 18.

Improved version:

function vehicleType(color, code, age) {
  const condition = age <= 1 ? 'new' : 'used';

  if (code === 1) {
    return 'This is a ' + condition + ' ' + color + ' car';
  }

  if (code === 2) {
    return 'This is a ' + condition + ' ' + color + ' motorbike';
  }

  return 'Sorry! This vehicle is unknown -_-';
}

console.log(vehicleType('red', 1, 1));
console.log(vehicleType('blue', 2, 7));
console.log(vehicleType('orange', 5, 2));

7-more.js

I would expect the result as indicated:

console.log(vehicleType('green', 4, 5)); // -> a green used bike

Check the assignment example. You have a one-off error.

What if I supply -1 for code?

a green used undefined

Looking at this code snippet:

if (code <= n) {
  return result;
} else {
  return console.log('Sorry! The vehicle is not available');
}

If I call your function like this:

console.log(vehicleType('green', 9, 5));

then I get the output shown below because the console.log function returns undefined.

Sorry! The vehicle is not available
undefined

I guess that is why you called the function without a console.log like this:

vehicleType('green', 9, 5);

But that would imply that I would have to know beforehand how to call the function depending on the parameters I supply. Why don't you just return a string so that you can always call the function in a standard way?

if (...) {
  return result;
} else {
  return 'Sorry! The vehicle is not available';
}

In fact, you don't need an else clause here because the else is never executed when the if block executes a return.

if (...) {
  return result;
} 

return 'Sorry! The vehicle is not available';

8-more.js

This is Tamim's solution, which I will not review.

9-more.js

This is your own solution. It has the problem that it makes assumptions on how many vehicles you have in the list. When working with arrays you should not make any such assumptions.

See my message in slack for alternative approaches.

10-more.js

Correct!

11-more.js

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.

12-more.js

Same reasoning as for 11.

13-more.js

Correct!

Feedback homework week 2

Hi Mudar, here is my feedback on your homework for week 2.

This homework is excellent, good work! I can't really find anything significant to comment about. Perhaps just these two minor observations.

1. In your week 2 homework it looks like the VSCode auto format settings were not yet added, as the formatting is a bit off in some files. However, a quick glance at your week 3 homework (which I will review next week) suggests that you added the settings afterwards.

2. It is customary in Europe to print the €-sign before the money amount. Although you can use an escape code to print a €-sign, you can also place it directly in the source code. If you can't find it on your keyboard (on Windows you can use the Character Map utility), you can also just copy and paste it, for instance from the assignment web page.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.