Insane
Home » Article » Javascript » Cara Mendapatkan Nilai Tertinggi/terendah Dari Array

Cara mendapatkan nilai tertinggi/terendah dari Array

f_huda19   10 Desember 2014   06:32:25   0 Suka   Favorit

Dalam sebuah bahasa pemrograman terkadang kita butuh sesuatu yg simple supaya kode kita terlihat efektif. Tetapi terkadang yg simple itu malah susah untuk di buat. Iya, Karena kita belum mengetahui fungsi apa yg harus digunakan.

Sebenarnya di Javascript sudah terdapat fungsi semisal Math.max() Dan Math.min() Jika mendengar tulisan itu pasti Anda sudah tau fungsinya untuk apa. Yaps, benar sekali. Untuk mendapatkan nilai tertinggi atau terendah.

Tapi jangan berharap Anda dapat menggunakan fungsi tersebut untuk mengecek nilai tertinggi/terendah dari suatu Array karena usaha Anda akan sia-sia saja. Jangankan Array, variabel aja tidak bisa.

Lantas, bagaimana solusinya? Beberapa waktu lalu ane sempat nyangkul di google untuk mencari cara yg pas di hati dan berharap tidak menemukan cara tradisional (kalau cara tradisional mah saya juga bisa)

Dan pada akhirnya ketemu juga dengan Stack Overflow. Maaf tidak ada link (lupa tidak ane bookmark) Nah, jika Anda juga membutuhkannya silahkan simak beberapa potongan kode berikut ini.

<script type="text/javascript">
var arr= new Array(1, 9, 3, 2);
var max= Math.max.apply(Math, arr);
var min= Math.min.apply(Math, arr);

document.write(max); // 9
document.write(min); // 1
</script>

Bagaimana? Terlihat simple bukan? Bahkan terlihat tidak berguna bagi orang-orang yg tidak membutuhkan hhe.. Saya rasa cukup sekian untuk artikel kali ini. Mohon maaf jika terdapat kalimat yg kurang berkenan di hati Anda.

KOMENTAR

Ellmeab

Ellmeab 08 Apr 18

Where Can I Buy Levitra In Massachusetts Buy Accutane No Rx <a href=http://cialiorder.com>cheap cialis no prescription</a> Resistance To Amoxicillin

oberry

oberry 26 Apr 18

Perditas laugh was hysterical. You surely dont think if Id got my hands on my inheritance that black arsehole would have got a cent of it. That was bait to get him to marry me.
So whats the real reason you invited us, apart from a desire to swim and break bread with three of the best-looking men in town?
<a href=http://gobi.com.sg>buy cake online</a> Mort had always prided himself on not panicking and usually not rushing into a situation without careful planning. In sixteen years of sharing the planet with his inferiors had no serious regrets. He knew he wasnt super intelligent—but he was observant, rational and logical, which was more useful. He didnt want fame; he wanted to be independent like professor Higgins, to live his life, free of strife, doing whatever he thought was best for him. And this meant keeping a safe distance from all humans he didnt know well. Especially strangers. Only… there was something about Hales smile that had lowered his defences. Was he losing his grip, or just feeling vulnerable after a trying afternoon? Whatever the reason he determined to be more on his guard than usual.