Object notes

A few notes about things I’ve seen in your code.

Three levels of sugar

Sugar free - tastes like it’s good for you

const point = (x, y) => {
  return { ‘x’: x, ‘y’: x };
};

Lightly sweetened

Properties are not actually evaluated so we can leave the quotation marks off.

const point = (x, y) => {
  return { x: x, y: x };
};

Note, you still need quotation marks if the property name isn’t a legal variable name, e.g. if it contains spaces or punctuation.

Super sweet

When we want to make an object with properties named the same as some variables and the values the values of the variables we can leave out the property names altogether.

const point = (x, y) => {
  return { x, y };
};

Note on returning objects

A lot of you wrote something like:

const o = { x: 10, y: 20 }
return o;

But you can put any expression after a return:

So you can just write this:

return { x: 10, y: 20 }

Values in objects

Similarly, the value positions of an object can be any expression so rather than this:

const x = (p1.x + p2.x) / 2;
const y = (p1.y + p2.y) / 2;
return { x: x, y: y };

You can just write this:

return {
  x: (p1.x + p2.x) / 2,
  y; (p1.y + p2.y) / 2
};

Throwback to strings

A lot of you are trying to use search when you should be using indexOf.

They do similar things but search is more powerful. Which means it isn’t always doing what you might think.

const s = ‘foo * bar’;

» s.indexOf('*')
4

» s.search('*')
Uncaught SyntaxError: Invalid regular expression: /*/: Nothing to repeat