下面的代碼返回數組中最大的數字。我試圖弄清楚如何從數組中獲得前3個數字,並且一直無法弄清楚。從數組中獲取最高3個數字?
我也想弄清楚如何列出相應的作業與他們的分數。目前H1的「你的最高分數」只顯示數字,並沒有通過工作(或var名稱的最高數字)。
有關如何完成此任何想法?
謝謝!
HTML:
<div>
<h3> Job 95- <span id="score95"> </span> </h3>
<h3> Job 96- <span id="score96"> </span> </h3>
<h3> Job 97- <span id="score97"> </span> </h3>
<h3> Job 98- <span id="score98"> </span> </h3>
<h3> Job 99- <span id="score99"> </span> </h3>
<h3> Job 100- <span id="score100"> </span> </h3>
<h1> Your highest score was: <span id="highest"></span></h1>
</div>
JS
var job95 = 100;
var job96 = 100;
var job97 = 100;
var job98 = 100;
var job99 = 100;
var job100 = 100;
$("#no15").click(function() {
console.log('clicked15')
job95 += 10;
job96 += 20;
job97 += 30;
job98 += 40;
job99 += 50;
job100 += 60;
$('#score95').html(job95);
$('#score96').html(job96);
$('#score97').html(job97);
$('#score98').html(job98);
$('#score99').html(job99);
$('#score100').html(job100);
var numArray = [job95,job96, job97, job98, job99,job100]
$('#highest').html(Math.max.apply(Math,numArray));
});
順序按得分,然後取頂部3? – tomliversidge
嘿@tomliversidge。不知道該怎麼做。有關如何獲得相應職位名稱的任何想法?謝謝您的幫助! – AndrewLeonardi
@AndrewLeonardi JS有一個內置的'sort'函數,你還需要知道些什麼? – Barmar