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...