javascript-tutorial / en.javascript.info

Modern JavaScript Tutorial
https://javascript.info
Other
23.05k stars 3.82k forks source link

Add explanation of Unary plus (+) operator to Extract for money strings exercise #3685

Open martinDolan opened 2 months ago

martinDolan commented 2 months ago

The solution to Extract the money problem, uses the unary + operator, however, I don't see that explained anywhere in the Strings section. Adding an explanation of that would be helpful.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus

function extractCurrencyValue(str) {
  return +str.slice(1);
}
Alexandre887 commented 2 months ago

I don't see that explained anywhere in the Strings section. Adding an explanation of that would be helpful.

This has already been mentioned in the article "Basic operators, maths", there is even a task on this topic. If a potential reader hasn't internalized the information in this chapter, and in the "JavaScript Fundamentals" section in general, I don't think reading the "Strings" article in the "Data Types" section will do him any good.

martinDolan commented 2 months ago

Understandable assuming everyone reads from beginning to end. I didn't read the Basic operators, maths section, but maybe a link to that would be useful to some.

Alexandre887 commented 2 months ago

Don't see much point to it. I don't think anyone resorts to solving such problems and using string methods without knowing such elementary things as unary plus. It is equivalent to explaining what an object is in the Classes chapter.

But, for that matter, then it would be more logical to replace +prompt(...) with Number(prompt(...)) instead of explaining the purpose of the $+$ in the notation. But even this is, in my opinion, an unnecessary measure.