Hackerrank challenge - Apple and Orange The problem I am trying to solve is this.  Apples fall at various offsets relative to an apple tree at position a, and oranges fall at various given offsets relative to an orange tree at position b.  How many of each kind of fruit lie between positions s and t?

I pass 8 from the 11 test cases, I fail 3 tests due to timeout error.

I spent the last hour or so trying to refactor my code to make it faster but I am out of ideas now.
Can someone give an advice what should I do in order to make it faster?

function countApplesAndOranges(s, t, a, b, apples, oranges) {
    let house = [];
    let applesCount = 0;
    let orangesCount = 0;
    for(let i = s; i &lt;= t; i++){
        house.push(i);
    };
    const applesPositions = apples.map(val=&gt; val + a);
    const orangesPositions = oranges.map(val=&gt; val + b);

    for(let j = 0; j &lt; house.length; j++){
        for(let k = 0; k &lt; applesPositions.length; k++){
            if(house[j] === applesPositions[k]) applesCount++;
        };
        for(let m = 0; m &lt; orangesPositions.length; m++){
            if(house[j] === orangesPositions[m]) orangesCount++;
        };
    };
    console.log(`${applesCount}${'\n'}${orangesCount}`);

};
Just a few if statements will do.
Your answer to the problem is rather strange.
To find out if an apple or orange is between s and t is just a simple if statement.
if(apple[index] + appleTreePos &gt;= s &amp;&amp; apple[index] + appleTreePos &lt;= t){ 
    // Apple is on the house.

// Same for the orange.

So you only need to loop over the apples and oranges once. Best is to have a single loop that does both the apples and oranges at the same time, then a second loop to do the remaining (if any) fruit.
There is no need to create any arrays. Your solutions is exponentially lots more work than is needed. For speed aim to reduce the number of times your code loops. Note that Array.map is the same as doing a for loop over each item, but much slower in this case.
NOTE I struck out exponentially and added &quot;lots&quot; as some have complained, I used it in the common vernacular (increasing rate of change) and not as in CS time complexity. 
Your code loops over the length of the house, then the number of apples and oranges, and then you loop the length of the house times the number of apples plus oranges.
If the house is 5 units wide and there are 5 of each fruit you iterate over them a total of (house pos to array) 5 + (map each fruit array) 5 + 5, (counting) 5 * (5 + 5) for a total of 65 iterations. This is 60 times too many.
Example
I have not run this code so there could be a few typos, but the general structure is as described above.
This is faster than your solution, If it is fast enough to pass the test I do not know.
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges) {
    var appleCount = 0;
    var orangeCount = 0;
    var i = 0;

    // find smallest group
    const len = Math.min(apples.length, oranges.length);

    // do apples and oranges
    while (i &lt; len) {
        let pos = apples[i] + appleTreePos;
        appleCount += pos &gt;= houseLeft &amp;&amp; pos &lt;= houseRight ? 1 : 0;
        pos = oranges[i++] + orangeTreePos;
        orangeCount += pos &gt;= houseLeft &amp;&amp; pos &lt;= houseRight ? 1 : 0;
    }

    // if same number of fallen then return result
    if (apples.length === oranges.length) {
        console.log(`${appleCount}${'\n'}${orangeCount}`);
        return;
    }

    // if more apples have fallen the do remaining apples 
    if (apples.length &gt; len) {
        while (i &lt; apples.length) {
            const pos = apples[i++] + appleTreePos;
            appleCount += pos &gt;= houseLeft &amp;&amp; pos &lt;= houseRight ? 1 : 0;
        }
        // all done return result
        console.log(`${appleCount}${'\n'}${orangeCount}`);
        return;
    }    

    // must be more oranges so do the rest of them and return result
    while (i &lt; oranges.length) {
        const pos = oranges[i++] + orangeTreePos;
        orangeCount += pos &gt;= houseLeft &amp;&amp; pos &lt;= houseRight ? 1 : 0;
    }
    console.log(`${appleCount}${'\n'}${orangeCount}`);
 }