JavaScript Temukan nomor terkecil dalam array
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(...arr)
console.log(min)
Yawning Yak
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(...arr)
console.log(min)
function smallestElemnts(numbers) {
var smallest = numbers[0];
for (let i = 0; i < numbers.length; i++) {
var elements = numbers[i];
if (elements < smallest) {
smallest = elements;
}
}
return smallest;
}
const numbers = [2, 3, 4, 8, 5, 2, 4];
console.log(smallestElemnts(numbers));
//Output: 2
//Not using Math.min:
const min = function(numbers) {
let smallestNum = numbers[0];
for(let i = 1; i < numbers.length; i++) {
if(numbers[i] < smallestNum) {
smallestNum = numbers[i];
}
}
return smallestNum;
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
// smallest number from array
function solution(num) {
let small = num[0];
for (let i = 1; i <= num.length; i++) {
if (small > num[i]) {
small = num[i]
}
}
console.log("smallest nunber:", small)
}
solution([3, 1, 4, 6, 5, 7, 3, 2,0,-1])
var arr = [5,1,9,5,7];
var smallest = arr[0];
for(var i=1; i<arr.length; i++){
if(arr[i] < smallest){
smallest = arr[i];
}
}
console.log(smallest);