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

305
Views
finding ratio of an array of intergers between 2 integers l and r. in javascript

You are given an array of integers a and two integers l and r. You task is to calculate a boolean array b, where b[i] = true if there exists an integer x, such that a[i] = (i + 1) * x and l ≤ x ≤ r. Otherwise, b[i] should be set to false.

function boundedRatio(a, l, r) {
  let b = []
  for (let i = l; i <= r; i++) {
    //  
    for (let j = 0; j < a.length; j++) {
      let result = (j + 1) * i

      if ((j + 1) * i === a[j] && l <= i <= r) {
        b.push(true)
      } else {
        b.push(false)
      }

    }
  }

  return b

  boundedRatio([8, 5, 6, 16, 5], 1, 3)
  // [false, false, true, false, true]

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

0

Your main loop should be over the array a. For each of them, you need a loop over l through r. In that loop you should just set a variable to inidcate whether the bounded value is found. Once you find it you can break out of the loop. Then you push that onto the result.

The main loop and push can be combine with map().

function boundedRatio(a, l, r) {
  let b = a.map((el, i) => {
    let bounded = false;
    for (let x = l; x <= r; x++) {
      if ((i + 1) * x == el) {
        bounded = true;
        break;
      }
    }
    return bounded;
  })

  return b;
}

console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3));

Here's how it looks with a for loop instead of map().

function boundedRatio(a, l, r) {
  let b = [];
  for (let i = 0; i < a.length; i++) {
    let bounded = false;
    for (let x = l; x <= r; x++) {
      if ((i + 1) * x == a[i]) {
        bounded = true;
        break;
      }
    }
    b.push(bounded);
  }

  return b;
}

console.log(boundedRatio([8, 5, 6, 16, 5], 1, 3));

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!