Search for a number in an array

Hi, guys, once again :slight_smile:

Here is a math problem I have been trying to solve and right after it is the code I wrote:


The program checking my code is only giving me 20% on the tests it runs with this code…Can you please tell me where my mistakes are? Also, I am open to reading your suggestions for more elegant solutions (I think this one with so many variables looks clumsy). I would appreciate a more ‘professional’ way of doing that with less code and more methods I gues…

Thanks in advance

If we de-structure the second array,

[a, b, c] = [10, 2, 3];

that gives us the three variables we need to work with.

 > x = [2, 2, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 2, 2]
<- (16) [2, 2, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 3, 2, 2]
 > x.slice(0, a).splice(b).filter(x => x === c).length
<- 4
 > c = 2
<- 2
 > x.slice(0, a).splice(b).filter(x => x === c).length
<- 0

Please don’t let this take the air out of your tires. It’s an abstraction of what you are doing above, and nothing more. You should cover the ground work you are doing, but be aware that iterators and methods have a bright side, namely that we can write them as expressions.

Hey! Thank you for taking the time to reply!

Can you please give me a little bit more information on

1 Like

The example given had little to work with so I supplied a longer array, x. The first two elements are two’s. The last two elements are also two’s, but that is moot, they are part of the group of elements that get truncated.

We have an array that is length, 10, as per the first variable, a, which we have sliced from the original array. Then we have an array that is length, 8, since we spliced out the first two elements, as per the variable b. From the remaining elements we filter out the search term, c and count it by polling the length property of the returned array.

When we searched for 3 the return was 4. There are indeed four three’s in the remaining eight elements. A search for 2 yields none, which is again what we would expect since the two’s have all been removed, albeit not specifically, more by circumstance.

Maybe get it working before looking for other things?

The instructions don’t mention anything resembling outputArray or resultArray, there’s no mention of interrupting repetition (break). But they’re in the code - the bug is in not having thought through what should happen.

The actions described are simple ones:

take x
drop x

The use of them is also simple, it goes something like: take this many, drop that many, then count this other thing.

If you do those things in sequence, one after the other, then no part of your code needs to be doing anything difficult.

You can also break out such actions into their own function, there is no reason that the use of some action and the implementation of that action needs to be in the same place.

function count( ... ) {

function take( ... ) {

step0 = input
step1 = take(a, step0)
step2 = drop(b, step1)
step3 = count(c, step2)
step4 = formatString( ... )

At this point it’s nearly solved, it’s not much more than that.

You should still get your all-in-one loop version correct, take it as an exercise in debugging. You’ll need to start by reading the instructions again though. After reading, you could write down the steps in english, as comments perhaps, and then under each comment you could write the corresponding code.

But if you can break the problem into smaller parts, then that’s usually a superior solution.