Generation

generate functionThu, 20 Jul 2023

In a library we want to know which book has the fewest pages and the one with the most pages. Luckily, no two books have the same number of pages. We need the function to receive an array of numbers, without ordering, and to return an array of two positions with the index of the book with the fewest pages and the index of the book with the most pages.

function bookSizes(arr) { var sortedArr = arr.sort(function(a, b) { return a - b; }); var small = sortedArr[0]; var big = sortedArr[sortedArr.length - 1]; var smallest = arr.indexOf(small); var biggest = arr.indexOf(big); return [smallest, biggest]; }

Javascript
Generate More

Questions about programming?Chat with your personal AI assistant