Ector
Ector

Reputation: 93

Each digit differs from the next one by 1

Script has to work this way:

isJumping(9) === 'JUMPING'

This is a one digit number

isJumping(79) === 'NOT JUMPING'

Neighboring digits do not differ by 1

isJumping(23454) === 'JUMPING'

Neighboring digits differ by 1

I have:

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        if (Math.abs(str[i+1]) - Math.abs(str[i]) == 1){
            return 'JUMPING';
        }
    }
    return 'NOT JUMPING';
}
console.log(isJumping(345));

Help please, where is mistake?

Upvotes: 0

Views: 255

Answers (5)

phhu
phhu

Reputation: 1962

A vaguely functional approach... The find gets position of the first character pair where the gap is more than one. The .filter deals with negatives (and other extraneous characters) by ignoring them.

// default b to a, so that last digit case, where b===undefined, gives true
const gapIsMoreThanOne = (a,b=a) => ( Math.abs(a-b)>1);
const isDigit = n => /[0-9]/.test(n);

const isJumping = n => n.toString()
  .split("")
  .filter(isDigit)
  .find((x,i,arr)=>gapIsMoreThanOne(x,arr[i+1])) 
  === undefined 
  ? "JUMPING" : "NOT JUMPING"
;

console.log(isJumping(1));         // JUMPING 
console.log(isJumping(12));        // JUMPING
console.log(isJumping(13));        // NOT JUMPING
console.log(isJumping(21));        // JUMPING
console.log(isJumping(21234565));  // JUPING
console.log(isJumping(-21234568)); // NOT JUMPING
console.log(isJumping("313ADVD")); // NOT JUMPING   

PS: To me "JUMPING" implies that there is a gap greater than one, not that there isn't: but I've gone with how it is in the question.

Upvotes: 0

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8168

Loop over the characters and early return with "NOT JUMPING" if the condition is violated & if the condition is never violated return "JUMPING".

function isJumping(num) {
  const strNum = String(Math.abs(num));

  for (let i = 0; i < strNum.length - 1; i++) {
    if (Math.abs(strNum[i] - strNum[i + 1]) > 1) {
      // replace `> 1` with `!== 1`, if diff 0 is not valid!
      return "NOT JUMPING";
    }
  }

  return "JUMPING";
}

console.log(isJumping(9));
console.log(isJumping(79));
console.log(isJumping(23454));

Upvotes: 1

Jamiec
Jamiec

Reputation: 136124

You just need to check your single digit case, and then see if all the digits vary by just 1

function isJumping(number) {
    let str = number.toString();
    if(str.length == 1)
      return 'JUMPING'
    
    const allByOne = str.substring(1).split('').every( (x,i) => {
      var prev = str[i];
      return  Math.abs( +x - +prev) == 1
    })
   return allByOne ? 'JUMPING' : 'NOT JUMPING';
}
console.log(isJumping(9));
console.log(isJumping(79));
console.log(isJumping(23454));

Upvotes: 0

tevemadar
tevemadar

Reputation: 13195

A clumsy way would be if (Math.abs(Math.abs(str[i+1]) - Math.abs(str[i])) == 1). Right now you are using Math.abs() to convert digits to numbers. Also, indexing is off, you start from 1, which is good, but then you should compare [i] and [i-1]. And the usual mismatch: you can say "JUMPING", only at the end. So you should check for !==1, and return "NOT JUMPING" inside the loop, and "JUMPING" after. That would handle the 1-digit case too.

It's a more readable practice to use parseInt() for making a number from a digit, otherwise the implementation of the comment:

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        if (Math.abs(parseInt(str[i-1]) - parseInt(str[i])) !== 1){
            return 'NOT JUMPING';
        }
    }
    return 'JUMPING';
}
console.log(isJumping(345));
console.log(isJumping(3));
console.log(isJumping(79));

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074585

There are a couple of issues:

  1. You're not handling single digits.
  2. You're returning too early. You're returning the first time you see a difference of 1 between digits, but you don't know that subsequent differences will also be 1.
  3. You're not checking the difference between the first and second digits, and you're going past the end of the string.
  4. You're using Math.abs as a means of converting digits to numbers.

Instead (see comments):

function isJumping(number) {
    let str = number.toString();
    for (let i = 1; i < str.length; i++) {
        // Convert to number, do the difference, then
        // use Math.abs to make -1 into 1 if necessary
        if (Math.abs(+str[i] - str[i-1]) !== 1) {
            // Found a difference != 1, so we're not jumping
            return "NOT JUMPING";
        }
    }
    // Never found a difference != 1, so we're jumping
    return "JUMPING";
}
console.log(isJumping(345));    // JUMPING
console.log(isJumping(9));      // JUMPING
console.log(isJumping(79));     // NOT JUMPING
console.log(isJumping(23454));  // JUMPING

In that, I use +str[i] to convert str[i] to number and implicitly convert str[i-1] to number via the - operator, but there are lots of ways to convert strings to numbers (I list them here), pick the one that makes sense for your use case.

You might also need to allow for negative numbers (isJumping(-23)).

Upvotes: 0

Related Questions