13

Arguing with a guy in a PR that substring(0,1) (first index inclusive, last exclusive) is equal to charAt(0), whereas he seems to think it's charAt(1).

My patience is wearing thin, but I now feel the need to check I'm not the moron here - someone please confirm if I'm the idiot here or not?

Comments
  • 4
    I'll guess JavaShit eh JavaScript.

    You're correct. charAt and substring are 0 indexed, thus equal as both return on invalid ranges an empty string and 0 is always first index.
  • 6
    Googling the js docs (glad I worked out the language!) I concur with you
  • 10
    Wouldn't trying this have been quicker than asking the question?
  • 2
    @AlmondSauce

    I'm not sure, but is CharAt and Substr multibyte safe?

    That could be problematic.
  • 1
    @dan-pud I did, but my confidence is still somehow lacking in case I've booked something up this ridiculously simple.

    Java though, I should have mentioned.
  • 2
    @AlmondSauce *pats your ego* don't get hard.

    *hrhrhhr*

    Na it's fine to look up stuff. Small things matter. Eg. Multibyte handling. Impossible to know all the caveats in all languages
  • 2
    Are you the reviewer or reviewee?

    If former, remove yourself and let him have fun fixing bugs later

    If latter, you could get someone sane enough to provide you the requisite approval
  • 1
    Just send him documentation?
  • 0
    @iiii Some people struggle to read it seems.
  • 2
    Use a unittest to prove the point.
  • 3
    w h a t

    This is a circus. Have this lazy ass to read the docs.
  • 2
    @TheCommoner282
  • 2
    Three ways to ask him would be

    a) "if you run "abc".substring(0,1)" in the DevTools console what do you get?

    b) look at the function definition on mdn. The second argument is optional. So obviously substring(0) gets the first item!

    c) If what YOU'RE saying is true - how would you get the first item? Would the first argument be set to -1? That would be weird wouldn't it?
  • 1
    PS: I truly hope you're not actually arguing about what a specific line of code does ... as it's so easy to test it in DevTools.

    I assume you're arguing about how to write a function that uses variables in place of the arguments.

    And this is what's causing confusion.
  • 3
    Open up jshell, do
    > var s = "hello";
    > s.charAt(0);
    'h'
    > s.charAt(1);
    'e'
    > s.substring(0, 1);
    'h'
    >

    and post him the output
  • 3
    Thanks guys, he's finally seen the light 😅
  • 2
    @AlmondSauce I thought you were instructed by HR to not kill people?
  • 2
    @IntrusionCM Shhh. Don't tell them.
  • 1
    Na ur fine. He's the moron. They are everywhere...
  • 1
    Don't ask us, ask the console.
Add Comment