Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created May 1, 2021 05:15
Show Gist options
  • Select an option

  • Save syedjafer/270bf165b47324eb4877d79f7d30f5d4 to your computer and use it in GitHub Desktop.

Select an option

Save syedjafer/270bf165b47324eb4877d79f7d30f5d4 to your computer and use it in GitHub Desktop.
function find_max_min(array){
var max_val;
var min_val;
if (array.length == 1){
max_val = array[0];
min_val = array[0];
}
else if(array.length > 1){
if (array[0] > array[1]){
max_val = array[0];
min_val = array[1];
}
else{
max_val = array[1];
min_val = array[0];
}
}
for (var itr=2; itr<array.length;itr++){
if (array[itr]>max_val){
max_val = array[itr];
}
else if(array[itr]<min_val){
min_val = array[itr];
}
}
console.log(min_val, max_val)
return [min_val, max_val]
}
var arr = [1, 2, 3, 4];
console.log(find_max_min(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment