lundi 17 décembre 2018

determining whether the a number is tidy (javascript)

So I am trying to create a function that determines whether a number is 'tidy'. I.e. each digit in the given integer is greater than the one that came before it. I have successfully managed to convert the integer into an array. But when I loop through it I don't get the desired true for tidy and false if otherwise. I am wondering what I am doing wrong?

function tidyNumber(n){
  var arr = n.toString(10).split("").map(function(t){return parseInt(t)});
  let tof;

  if(arr.length == 1){
    return true;
  }

  for(i = 0; i < arr.length; i++){
    if(arr[i] <= arr[i+1]){
      tof = true;
    } else if(arr[i] > arr[i+1]){
      tof = false;
   }
  }
  return tof;
}

Aucun commentaire:

Enregistrer un commentaire