Return value if criteria is not met in javascript .some() function -
given array of numbers, wish use javascript's .some() method find first pair of mirror image numbers , return pair.
e.g. [454,86,57,75,16,88]) should return [57,75]. if there no numbers mirror each other, want return [-1,-1]. managed point if there mirror image numbers, numbers returned. however, can't return [-1,-1]. did go wrong?
var a=0 var b=0 arr=[13,15,19,32,49,61,73] arr.some((x,i)=>{ a=arr[i] b=arr[i+1] b= switchit(b) if(a!==b){ //what if there no mirror image numbers found? = -1 b = -1 }else{ return a==b } }) function switchit(n){ n= n.tostring() n= n.split("") var [x,y,z]=[n[0],n[1],n[2]] n= [z,y,x] n= n.join("") n= parseint(n) return n } console.log([a,switchit(b)])
one simple ajust , code work expected:
arr.some((x,i)=>{ var end = >= (arr.length - 1); a=arr[i] if (!end) { b=arr[i+1] b = switchit(b) } if(a!==b || end){ //note or end variable here = -1 b = -1 }else{ return a==b } })
the ajust consists in test when function find end of array
Comments
Post a Comment