Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to find the min and max of an int array in JavaScript

3 Answers

0 votes
const array = [3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7];

const min = Math.min(...array);
const max = Math.max(...array);

console.log(`Minimum: ${min}`);
console.log(`Maximum: ${max}`);



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz
0 votes
const array = [3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7];

let min = array[0];
let max = array[0];

const size = array.length;

for (let i = 1; i < size; i++) {
    if (array[i] < min) {
        min = array[i];
    }
    if (array[i] > max) {
        max = array[i];
    }
}

console.log(`Minimum: ${min}`);
console.log(`Maximum: ${max}`);



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz
0 votes
const array = [3, 14, 4, 1, 5, 90, 2, 6, 89, 3, 7];

const min = array.reduce((a, b) => Math.min(a, b));
const max = array.reduce((a, b) => Math.max(a, b));

console.log(`Minimum: ${min}`);
console.log(`Maximum: ${max}`);



/*
run:

Minimum: 1
Maximum: 90

*/

 



answered Jan 16, 2025 by avibootz

Related questions

1 answer 93 views
3 answers 133 views
1 answer 74 views
1 answer 84 views
1 answer 74 views
1 answer 70 views
...