Find the largest negative number in an array

x = [-10,-6,-5,-3];

Please someone tell me how I can search the largest negative number in an arry;

First, I would change the category from general to javascript.

Here is my pseudocode.

  1. Define a variable and store a number.
  2. Run a forloop over your array and compare stored number with each value in the array.
  3. If the current value is bigger than the stored value, replace the stored value with current value.
  4. Repeat.

Hope this helps!

function bigFun(num){
    let x = [0];
    let y = [];
     
    for(let i=0;i<num.length;i++){
        for(let j=0;j<num[i].length;j++){
            if(num[i][j] > x[0]){
                x = [num[i][j]];
            };
        };
        y.push(x[0]);
        x = [0];
    };
    console.log(y);
};

Well, this is my code, but It’s can only find positive number
like

let arry = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]
bigfun(arry)
result is [27, 5, 39, 1001]

however

let arry = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]

the function can’t find the solution because in 4th array, there is negative number
So, how can I solve this without using math.max();

In this case you should do it with the loop, so you can learn better how this work
You are using 0 to compare, you can compare things with something else and you will be able to solve it in this way

You don’t need to make x in array, you can totally make it just a number, making an array and always referencing x[0] make things more complex
If you initialise the variable x inside the first loop you dont need to set it back to the initial value after pushing it, as it would be initialised again in the following iteration - just think to what value you need to initialise it as

To use Math.max to find the max number in an array you would also need the spread operator, but it is something you will reach later
I couldn’t know you where referring to this challenge nor what issue you were having
This show the importance of being thorough when asking for help, and showing your whole code

1 Like