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;
};
static int SmallestNumberin(int[] numbers){
int smallestNumber = numbers[0];
for (int i = 1; i < numbers.Length +1; i++)
{
if(numbers[i] < smallestNumber){
smallestNumber = numbers[i];
}
}
return smallestNumber;
}
Array.min = function( array ){
return Math.min.apply( Math, array );
};
var arr = ['cats', 'giants', 'daughters', 'ice'];
var min = Math.min(...arr.map(({ length }) => length));
console.log(min);