2

I want to get 2 smallest numbers out of 3 numbers and then want to display that 2 smallest numbers on the screen simply like in PHP we use simply echo to display the value on the screen. But with this code I get only one smallest value need to get and display 2 smallest values on the web page.

<script  type="text/javascript">
  var a = 5;
  var b = 4;
  var c = 6;
  var d = Math.min(a,b,c);
</script>

so the result should be "First smallest: 4" and "second smallest: 5"

Put these first & second lowest values in different variables like var first = 4 & var second = 5 and then should display like with PHP code <?php echo $first;?> and <?php echo $second ; ?>.

halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

5
var n = 2; // size of min you want
var array = [2,3,4,5] , outarray=[];
var min;
for(i=0;i< n;i++){
    min = Math.min.apply(Math,array); 
    outarray.push(min);
    array.splice(array.indexOf(min), 1);
}
alert(outarray);

JS Bin link

Kayis Rahman
  • 352
  • 3
  • 13
1

Do it like below: (Pure js)

First sort the number array then loop over to find the required values

function LowestAndSecondLowest(arr_num)
{
  arr_num.sort(function(x,y)
           {
           return x-y;
           });
  var uniqa = [arr_num[0]];
  var result = [];
  
  for(var j=1; j<arr_num.length; j++)
  {
    if(arr_num[j-1] !== arr_num[j])
    {
      uniqa.push(arr_num[j]);
    }
  }
  
  result.push("First smallest: "+uniqa[uniqa.length-arr_num.length],"second smallest: "+uniqa[1]);
  
  return result.join(' & ');
  
}
document.write(LowestAndSecondLowest([4,5,6]));
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
  • hello i need to pass the variable names a,b,c as per my question variable names used here document.write(LowestAndSecondLowest([4,5,6])); in place of 4,5,6. Because these values will change everytime and the result should display on the same screen. please use the variable names reply me others fine. thanks & Regards. – user7423894 Feb 09 '17 at 06:54
  • you can pass variables like : `var a=4; var b=5; var c=6; alert(LowestAndSecondLowest([a,b,c]));` @user7423894 – Ambrish Pathak Feb 09 '17 at 07:00
  • sir can you please tell me that how can i get these 2 smallest values in 2 different variables in jQuery . Like 4 in var name = first & 5 in var name = second . please reply me.Thanks & Regards – user7423894 Feb 09 '17 at 07:10
  • and one thing more sire if the first & second variable values are nothing then display there 0 should not be null & should not be undefined.Regards – user7423894 Feb 09 '17 at 07:14
  • `var a = uniqa[uniqa.length-arr_num.length];` and `var b = uniqa[1];` – Ambrish Pathak Feb 09 '17 at 07:16
  • hello can you please tell me that how can i get var b = uniqa[1]; var b value & display with php eco . like . bcz i want to display the var b value with this .Please reply me .Thanks & Regards. – user7423894 Feb 09 '17 at 07:48
  • hello sir var a = uniqa[uniqa.length-arr_num.length]; and var b = uniqa[1]; this is not working aftyer that i am displaying these values with alert(a); & i get no results with this even there should be 4 in alert box for var a. please check this once & fix this in your above example so that i can check there that whats wrong im doing on my side. thanks & regards – user7423894 Feb 09 '17 at 08:04
0

You can find maximum and remove it from array:

var a = 5, b = 4, c = 6;
var ar = [];
ar.push(a, b, c);
var max = Math.max(a, b, c);
ar.splice(ar.indexOf(max), 1);
var min1 = ar[0];
var min2 = ar[1];
Banzay
  • 9,310
  • 2
  • 27
  • 46