Live data from Hacker News

Stranger Things, JavaScript Edition

livecodestream.dev

51–58 of 58 posts

Re: Stranger Things, JavaScript Edition

#51
post #30
post #28

Earlier quoted context omitted.

But you're not getting the wrong answer, it's doing exactly what it should do. That one isn't really even a weird language quirk like the others. It's just assuming you're not aware that parseInt can take a 2nd argument

The issue doesn't arise because of parseInt() being able to take more than one argument, the issue is that map() is passing not only the value in the array but also its index as well as a copy of the full array. I use map() all the time but I didn't know about that until I read this article. I wonder why map() functions this way in Javascript and if any other languages pass additional values like this in their own ma…

> map() is passing not only the value in the array but also its index as well as a copy of the full array. I use map() all the time but I didn't know about that until I read this article

Then you don't know the basics of the language you're using- how can you complain about it?

> when I use map() I expect it to take each value in the array and pass it to the callback function. I don't expect...

map is well documented. Making the element's index available is indeed pretty useful (the entire array less so, but sometimes it can be).

Re: Stranger Things, JavaScript Edition

#52
i've been reading the "professional javascript for web developers" by matt frisbie. matt has done a great job, the book is very thorough. after reading through about a 1/3 of the book it's evident that js is a very flexible language and riddled with soooo many inconsistencies due to all the band-aids manily due to the evolution of the js standard. the section of var blew my mind. it's really sad that so many engineers are exposed to this mess. there is a reason there are so many languages that compile to js. with this pattern, it boggles my mind that the js standards folks continue to add more cruft into the language. they've done enough damage.

Re: Stranger Things, JavaScript Edition

#53
post #12

Common confusion has to do with how JS overloads the "+" operator. '9' + 1 = "91" +'9' + 1 = 10 It's all very weird though...

What would you expect string + number to do?

In this case, they could be copying Java which allows an implicit cast from number to string.

Re: Stranger Things, JavaScript Edition

#54

Earlier quoted context omitted.

I have a bit of a hard time with you there, I like to use maps in my code and trying to map a parser over a list of strings and getting the wrong answer is far from 'of no consequence' to me. I'm not saying these issues make the language unusable, but you've got to be a bit more honest that you were if you want to be taken seriously.

"If I want to be taken seriously?" Was that bit of petty oneupmanship really necessary? I assure you, Alex, I'm completely unconcerned with whether you take me seriously. You were probably just trying to show off. But in case you weren't, here's how JS works. In JavaScript, if you're passing in a function as a parameter, the normal way is to use an arrow function. Like this: `array.map((element) => yourFunction(eleme…

I am an amateur dev and used the arrow notation only in the way you described in your first example: array.map((element) => yourFunction(element))

What exactly is the shortcut you mention? (wild your mind converting the example into the incorrect/dangerous version?)

Re: Stranger Things, JavaScript Edition

#55

Earlier quoted context omitted.

Kind of depends on the language. Perl converts the string to an integer if you use a math operator on a numeric string. Python just throws an error.

When I want either addition or string concatenation, I know it, I can't think of a use case where I'm happy getting either randomly. Perl got this right: 2 + 2 == 4 2 . 2 eq 22 You can even declare that failed conversions should throw: use warnings FATAL => qw(numeric)

Another thing I thought was clever about Perl is it has two comparison operators, '==' for numeric values and 'eq' for strings.

Re: Stranger Things, JavaScript Edition

#56

Earlier quoted context omitted.

I have a bit of a hard time with you there, I like to use maps in my code and trying to map a parser over a list of strings and getting the wrong answer is far from 'of no consequence' to me. I'm not saying these issues make the language unusable, but you've got to be a bit more honest that you were if you want to be taken seriously.

"If I want to be taken seriously?" Was that bit of petty oneupmanship really necessary? I assure you, Alex, I'm completely unconcerned with whether you take me seriously. You were probably just trying to show off. But in case you weren't, here's how JS works. In JavaScript, if you're passing in a function as a parameter, the normal way is to use an arrow function. Like this: `array.map((element) => yourFunction(eleme…

It wasn't meant to be oneupmanship, just that you were deliberately ignoring the core of the argument. I could make an article about why NULL in C is a wonderful feature and how it allows such nice optimizations, or I could acknowledge that it has a great cost too. You basically said "There's no downside if you are as smart and experienced as me", but with respect to Javascript, much of the world (myself included) are not. Your argument came down to "people with sufficient skill don't make this mistake", which was definitional instead of actually addressing that the default behavior of an actual usecase was not sane.

Re: Stranger Things, JavaScript Edition

#57
post #33

Earlier quoted context omitted.

I have a bit of a hard time with you there, I like to use maps in my code and trying to map a parser over a list of strings and getting the wrong answer is far from 'of no consequence' to me. I'm not saying these issues make the language unusable, but you've got to be a bit more honest that you were if you want to be taken seriously.

Good linter will warn you that you are using parseInt without second parameter.

That's a fair criticism of my confusion, but I think I made my point poorly. The way map is implemented may be useful for some usecases and more convenient to implement, but it is a a very poor fit for many other usecases.

As an example of one of the many ways to solve this to make it more sane, the Rust language allows you to call .iter() on something that can be looped over, but also .enum(), which gives you an index and the item. In this way, you can manage expectations about what you are actually getting. Javascript hands you all the tools at once unconditionally which strips a lot of the abstracting power away.

Re: Stranger Things, JavaScript Edition

#58

Earlier quoted context omitted.

`072` is interpreted as octal, and `058` is interpreted as decimal (7·8+2 = 58). Ah, the dumpster fire that is JavaScript.

Prefixing octal numbers by zero was a common convention. The real dumpster fire was all those intermediate years without an octal notation in strict mode. (If you needed them, either forget strict mode, or choose between converting to meaningless decimals or using strings and feeding them through parseInt. What's sane about this?) However, 058 should have been a parse error.

I agree. That inconsistency in how the `0` prefix is handled shouldn't exist.
Post reply on HN