I am working on this problem and it seems to be getting the best of me.
My code is to execute a function that returns the highest value without using the Math.max()
function.
var arr = [42,13,34,8, -9, 3];
function max(array, selector) {
var val = array[0];
for (var i = 1; i < array.length; i++) {
if (selector == "Max") {
if (array[i] > val) {
val = array[i];
}
}
}
return val;
}
console.log(max([42,34,8,13,-9]))
I am working on this problem and it seems to be getting the best of me.
My code is to execute a function that returns the highest value without using the Math.max()
function.
var arr = [42,13,34,8, -9, 3];
function max(array, selector) {
var val = array[0];
for (var i = 1; i < array.length; i++) {
if (selector == "Max") {
if (array[i] > val) {
val = array[i];
}
}
}
return val;
}
console.log(max([42,34,8,13,-9]))
Share
Improve this question
edited May 4, 2020 at 20:30
j08691
208k32 gold badges269 silver badges280 bronze badges
asked May 4, 2020 at 20:27
Billy.Brown0618Billy.Brown0618
371 silver badge9 bronze badges
4
-
You're not passing in the
selector
parameter when you callmax
. Callmax([42,34,8,13,-9], 'Max')
. – Heretic Monkey Commented May 4, 2020 at 20:29 - In what way is this code not working as expected? This is a good opportunity to start using your browser's debugging tools. With the script debugger you can pause execution on a specific line of code and step through the code line by line as it executes, observing the runtime behavior and changing values of your variables. When you do this, which specific line of code first produces an unexpected result? – David Commented May 4, 2020 at 20:30
-
1
what is
selector
doing? – Nina Scholz Commented May 4, 2020 at 20:30 - 1 The premise is to this: Write a function called max, which accepts an array and returns the highest value. Do not use the built-in Math.max() function! The code above was one that I used and altered, but I am not pletely understanding why I have to use selector at all @NinaScholz – Billy.Brown0618 Commented May 4, 2020 at 20:36
6 Answers
Reset to default 5You can do it using the reduce
function:
function max(array) {
return array.reduce((prev, curr) => prev > curr ? prev : curr, undefined)
}
console.log(max([42,34,8,13,-9]))
console.log(max([-1, -50,-9]))
console.log(max([]))
You could just take a check and assign the actual value.
function max(array) {
var val = array[0];
for (var i = 1; i < array.length; i++) {
if (array[i] > val) val = array[i];
}
return val;
}
console.log(max([42, 34, 8, 13, -9, 101]));
This is what I have. I reached out to many people and they helped me settle on this. Thanks to everyone! This leads to my next question: How is this turned into finding the minimum? Wouldnt I just replace (<) with (>) and use i--? And if not, why not?
function max(arr) {
let maximum = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > maximum) {
maximum = arr[i];
}
}
return maximum;
}
I think that's a perfect use case for Array#reduce
. Here's a recursive answer in case you find it useful:
const max =
([x, ...xs], m) =>
x === undefined
? m
: max(xs, m > x ? m : x);
console.log(max([42,34,8,13,-9]));
console.log(max([]));
function findLargestNumb() {
let maxNumb = [0];
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] > maxNumb) {
maxNumb = arguments[i];
}
}
console.log(maxNumb);
}
findLargestNumb(1, 2, 3, 4, 5);
you can use the sort function and then return the first element in the array.
let arr = [42,13,34,8, -9, 3];
arr = arr.sort(sorter);
console.log(arr[0]);
function sorter(num,nextNum){
if(num>nextNum)
return -1;
else if (nextNum > num)
return 1;
return 0;
}