2

I successfully solved the "One Decremented" challenge on Coderbyte, but am wondering why one solution works and the alternative that I tried doesn't. the challenge is to have the function count how many times a digit appears that is exactly one less than the previous digit. (ie: '56' should return 0; '9876541110' should return 6)

This solution works (it logs 6):

var str = '9876541110'

function oneDecremented(str) {

  var split = str.split('')
  var total = 0

  for(let i = 0; i < split.length; i++) {
    if(split[i+1] == (split[i] - 1)){
      total += 1
    } else {} 
  }

console.log(total)

}

oneDecremented(str)

But if I change the first if statement to the below, the function logs an incorrect 0 for any entry:

if(split[i] == (split[i+1] + 1))

Would anyone be able to help me understand why that is? I'm sure it's a very basic answer that I'm somehow missing...

isherwood
  • 58,414
  • 16
  • 114
  • 157
May
  • 321
  • 1
  • 3
  • 4
  • 1
    A side point, while it doesn't break in javascript, you are running off the ends of your array boundaries and it would be good practice to be aware of that. Your loop runs from 0 to 9 (for the example you give), so for the last cycle of the loop you are comparing `split[10]` to `split[9] - 1` which will be `undefined == -1`. In JS this causes no errors, but you are wasting a cycle through your loop that you didn't need. And in other languages that check boundaries this would absolutely break. So your loop condition really ought to be `i < split.length-1` – Matt Burland Oct 12 '20 at 19:00
  • Yes, I was also wondering if that was something to avoid. Good to know. Thanks! – May Oct 12 '20 at 19:59

2 Answers2

2

So your mistake here, and it's a very common mistake, is that you are mixing up addition with string concatenation.

When you do this:

if(split[i] == (split[i+1] + 1))

You are comparing, for example 9 with 8 + 1. But 8 isn't a number, it's a string. And when Javascript sees a string followed by the + operator, it does string concatenation. So instead of comparing 9 with 9, you are comparing 9 with 81

This doesn't happen with the - example because the - operator doesn't work on strings. So Javascript automatically converts both strings to numbers for you and does the subtraction as expected

One kinda goofy way to fix it would be to do this:

if(split[i] == (+split[i+1] + 1))

Putting a + before the string split[i+1] will force javascript to treat split[i+1] as a number because there isn't a unitary string + operator. Do note, however, this adds nothing to the readability of your code and you really might be better served using the - version instead.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
2

The if statement if(split[i] == (split[i+1] + 1)) can also work after some minor changes.

First, as your array split[] is an array of characters, the part where you do split[i+1] + 1 can return an expected result because it will concatenate since, at first, you are trying to increment a character.

To solve this, you could just convert the string elements to integers when making the comparisons, for example:

if(parseInt(split[i]) === parseInt(split[i+1])+1) {...}

I would also avoid using == and use === instead, since === doesn't do type coercion. I hope this answer helps.

ptorr
  • 83
  • 6