Return a new array that is greater than its 2nd value

555 Views Asked by At

How to return a new array that its values are greater than its 2nd value. If the array that was passed to the function has less than two elements, function should return false.

For example,

greaterThanSecond([1,3,5,7])

should return [5, 7].

greaterThanSecond([0, -3, 2, 5])

should return [0, 2, 5].

greaterThanSecond([2])

should return false.

This is what I tried.

function valGreaterThanSecond(arr) {
  for (let newArr of arr) {
    if (newArr > arr[1]) {
      return [newArr]
    }else {
        return false
        }
    }
 }
5

There are 5 best solutions below

0
Atena Dadkhah On

You can try this:

const greaterThanSecond = arr => {
   if (arr.length > 1){ 
      return arr.filter(e => e > arr[1])
   }
   return false
}
console.log(greaterThanSecond([1,3,5,7]))

In this function, at first you should check if the length of the array is not less than 2. Then we filter the array by checking if each number in the array is bigger than the second number and keep those ones in the array.

0
Ádám Jakab On

You can try a one-liner:

[0, -3, 2, 5].filter((element, index, array) => element > array[1])

The filter function has 3 parameters:

  1. The inspected elment
  2. Index of the inspected element
  3. The original array

The filter iterate through the array and you can compare to the original array's second element

0
HiddenOne1254 On
function valGreaterThanSecond(arr) {
  
  let resultArray = []
  console.log(arr.length)
  let checkValue = arr[1]
  if(arr.length < 2)
  return false
  else
  {
  for(let i = 0 ; i < arr.length ; i++){
      if(arr[i]!=checkValue && arr[i]>checkValue)
      resultArray.push(arr[i])
  }
  return resultArray
}
 }
console.log(valGreaterThanSecond([2]))

try this approach

0
Sajjad Bayat On

You can try this

function greaterThanSecond(arr) {
    if (arr.length < 2)
        return false;
    return arr.filter((item) => item > arr[1])
}
console.log(greaterThanSecond([0,-3,2,5]))

0
Ben On

First let's try fixing your code before I am suggesting another approach.

function valGreaterThanSecond(arr) {
  let newArr = [];
  if (arr.length < 2) return false;
  for (let elem of arr) {
    if (elem > arr[1]) {
      newArr = [...newArr, elem]
      }
    }
  return newArr;
 };

 console.log(valGreaterThanSecond([1,3,5,7]));

The problem with your function is that once you found a number that is greater than the second element you immediately return it, thus exiting the function and returning this one element in an array, which is not what you want. If you found an element in the array that is not greater than the second element you immediately return false, which is not the behavior you want as well. In your example of [1,3,5,7] you will return false because 1 < 3 and you are out of the function at this point.

Different approach using reduce

I would like to suggest a different approach since there are a few answers using filter which is the first thing I would think about myself.

Here is a solution using the reduce function for arrays.

const greaterThanSecond = (arr) => {
    if (arr.length < 2) return false;
    const result = arr.reduce((acc, curr) => {
        if (curr > arr[1]) return [...acc, curr];
        return acc;
    }, []);
    return result;
}

console.log(greaterThanSecond([1,3,5,7]));