?? JS interview problem How to check if the elements of an array are in sorted order Day-5 ! ??

let arr = [200,30,40,50];
let increase=1;
let output = "array is sorted";
for(i=0; i< arr.length-1;i++){
 	 
  if(arr[i] > arr[increase++]){
    output = "array is not sorted"
  } 
}
console.log(output)        


In this JavaScript function isSorted, we iterate through the array and check if each element is less than or equal to the next element. If we find an element that is greater than the next one, we return false, indicating that the array is not sorted. Otherwise, if the loop completes without finding any such elements, we return true, indicating that the array is sorted.

Feel free to use and adapt this function according to your needs! Happy coding! ???? #JavaScript #Algorithms #ProgrammingTips

Akash Gupta

Angular Developer at Ameotech Informatics Actively looking for a new role

1 å¹´

let num = [121, 20, 55, 100, 199]; function checkNum(num, i = 0) { ?if (i === num.length - 1) { ??return "This is sorting Array!"; ?} ?if (num[i] > num[i + 1]) { ??return "This is not sorting Array!"; ?} ?return checkNum(num, i + 1) } console.log(checkNum(num));

let abc = [250,100,3451,525,4]; function xyz(abc) { for (i = 0; i < abc.length; i++) { if (abc[i] >abc[i +1]) { return "array is not sorted" } } return "array is sorted" } console.log(xyz(abc))

赞
回复

要查看或添加评论,请登录

Sanjeev Kumar的更多文章

社区洞察

其他会员也浏览了