Why the loop stop checking the other digits to verify the condition

function finddigit(digitalOfNumbers){

    for(let i=0;i<digitalOfNumbers.toString().length;i++){

    if(digitalOfNumbers.toString()[i]-digitalOfNumbers.toString()[i+1]!==-1) {

      return(false)

    }

    else{

      return(true)

    }

    }

    }

    console.log(finddigit(1245869))

what exactly is the issue with your code? but fundamentally, i don’t think it is appropriate you apply “.length” to a an array that is already converted to a string. You might need to take out the “toString()” array method for the condition segment of the loop and that of the "if " condition embedded in the loop too.

you provide an incomplete code snipped, but i assume your loop is found inside a function, as you return values. Once a function returns a value, it stops, any code found inside it, past the return statement is not executed. If you want your loop to run entirely, you shouldnt stop the function(return value) inside it. .

PS: instead of performing the Number.toString() method several times in your code, its better practice to store it in a variable and refer to the variable instead, ofc unless this would produce some unwanted side effects to your results.

let stringOfDigits=digitalOfNumbers.toString()

stringOfDigits.length
stringOfDigits[3]
...

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.