Sorting in decreasing order. Logical this code seems correct. What do i mis? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Sorting in decreasing order. Logical this code seems correct. What do i mis?

var array =[]; for (i = 0; i < 5; i++) { array[i] = Number(prompt("Press the " + (i +1) + " number")); } for(j = 0; j < array.length - 1; j++) { var h = 0; for(i = 0+h; i < array.length; i++) { var k = 0; var max = array[j]; if (max < array[i]) { max = array[i]; k = i; } } h++; var exchange = array[j]; array[j] = max; array[k] = exchange; }

21st Apr 2017, 9:40 PM
Pozik
Pozik - avatar
7 Answers
+ 1
var array =[]; for (i = 0; i < 5; i++) { array[i] = Number(prompt("Press the " + (i +1) + " number")); } for(j = 0; j < array.length - 1; j++) { var k = j; var max = array[j]; for(i = j+1; i < array.length; i++) { if (max < array[i]) { max = array[i]; k = i; } } var exchange = array[j]; array[j] = max; array[k] = exchange; } document.write(array);
21st Apr 2017, 10:35 PM
Pozik
Pozik - avatar
+ 22
Should the last line be array[j]=exchange? I'm not sure though 😕 k is declared within the previous loop.
21st Apr 2017, 9:49 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
document.write(array);
21st Apr 2017, 9:42 PM
Pozik
Pozik - avatar
+ 1
And yes i know about method "sort()".
21st Apr 2017, 9:43 PM
Pozik
Pozik - avatar
+ 1
No, will not work.
21st Apr 2017, 9:54 PM
Pozik
Pozik - avatar
+ 1
When i trying click to the "like" it writes " no conection". So i cant give you like or unlike)))
21st Apr 2017, 10:29 PM
Pozik
Pozik - avatar
+ 1
Now it working) thank you Yasmin.
21st Apr 2017, 10:36 PM
Pozik
Pozik - avatar