Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
how to find elements of a numeric array that are greater than the elements in front of them

array: const data = [158,164,742,99,155,250,240,87]

It is necessary to output to the console numbers that are greater than the previous ones: console.log(164 742 155 250)

const data = [158, 164, 742, 99, 155, 250, 240, 87]
function biggerElement(data) {
  for (let i = 1; i < -1; i++) {
    if (data[i] > data[i - 1]) console.log(data[i])
  }
}

how to solve this task? thanks

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use .flatMap() and compare current with future values:

x < a[i+1] ? a[i+1] : []

It has come to my attention that there's very little use for .flatMap() as a filter.

Then why not use .map() since .flatMap() is essentially the same thing but with one extra step? That extra step allows me to think a straight forward logic in which I can use simpler algorithms and not have to worry about the common side affects when dealing with arrays like getting empties. Here's a version of example #1 vs. a version using .map().

const data = [158, 164, 742, 99, 155, 250, 240, 87];

let resA = data.flatMap((x, i, a) => x < a[i + 1] ? a[i + 1] : []);

console.log(resA);

let resB = data.map((x, i, a) => x < a[i + 1] ? a[i + 1] : '');

console.log(resB);

So both .map() and .flatMap() are required to return after every iteration, but because .flatMap() has a built-in .flat() method, you can return an empty array which becomes nothing which is better than an empty like '', or undefined.

How about .filter() which of course is the most obvious answer? Compare Vectorjohn's callback, vs. my callback:

i > 0 && x > a[i - 1] // Vectorjohn, using .filter()

x < a[i + 1] ? a[i + 1] : [] // Me, using .flatMap()

Using .filter() you actually have no need to worry about blowing past zero and returning undefined because .filter() never returns falsey data only truthy data. So his callback can simply be the backwards version of my callback by removing i > 0 &&:

x > a[i - 1] // .filter() will return x

x < a[i + 1] ? a[i + 1] : [] // .flatMap() will return a[i + 1]

My callback doesn't eliminate undefined automatically so amittedly it is more verbose than Vj's callback (once I corrected it). But, .flatMap() can do far more than .filter(). While .filter() always returns the current value (x) on each iteration, .flatMap() can return anything (a[i + 1]), multiple anythings (["any", x]), or nothing ([]) on each iteration.

Example 1

const data = [158, 164, 742, 99, 155, 250, 240, 87];

let res = data.flatMap((x, i, a) => x < a[i + 1] ? a[i + 1] : []);

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

const data = [158, 164, 742, 99, 155, 250, 240, 87]

let numbers_to_print = ''

data.forEach(number => {
  const index = data.indexOf(number)

  if (number > data[index - 1]) {
    numbers_to_print += `${number} `
    // you can also just console.log(number), but I like to use a string
  } 
})

console.log(numbers_to_print)

about 4 years ago · Juan Pablo Isaza Report

0

The array filter method runs a function for each element in an array, and returns only the elements for which the function returns true. It receives the index of the current element. So you can access the previous element in your filter callback.

data.filter((value, i) => i > 0 && value > data[i - 1])

filter also receives the original array as the third argument, so you could use that instead of accessing data directly if that matters for your case, like if you wanted to make this helper reusable:

const biggerThanPrevious = (value, i, array) => i > 0 && value > array[i - 1]
// ... then somewhere else
const data = [158,164,742,99,155,250,240,87]
console.log(data.filter(biggerThanPrevious))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!