# Is my code efficient? Where do I Belong(Basic Algorithm Scripting)

**URL:** <https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 20, 2021, 3:49pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714 "2021-01-20T15:49:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [January 20, 2021, 3:49pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/1 "2021-01-20T15:49:48Z")

</div>

Here is the problem, [Basic Algorithm Scripting: Where do I Belong](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong)  
Here is my code,

```auto
function getIndexToIns(arr, num) {
  var sortArr = arr.sort(function(a,b){return a-b});
  var perfectNum = 0;
  for(let i=0; i < sortArr.length; i++){
    if(arr[i] >= num){
      perfectNum = i;
      break;
    } else if(num > sortArr[arr.length-1]){
      perfectNum = arr.length;
    }
  }
  return perfectNum;
}

```

Is my way of solving this issue is efficient? Feel free to share if you have any better process? 🤗

Some Useful link:

1. [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

2. [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

3. [Array.prototype.length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 4:07pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/2 "2021-01-20T16:07:28Z")

</div>

Sure, that is a good basic solution. Is it the best solution? That is subjective, but there is certainly a more efficient solution. Can you imagine a way to do this without sorting, making only one pass through the data?

---

<div class="post-metadata">

**Author:** ![alkapwn3d](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alkapwn3d/32/92787_2.png) [@alkapwn3d](https://forum.freecodecamp.org/u/alkapwn3d)\
**Post date:** [January 20, 2021, 4:25pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/3 "2021-01-20T16:25:05Z")

</div>

[![](https://img.youtube.com/vi/KXJSjte_OAI/maxresdefault.jpg "How Binary Search Makes Computers Much, Much Faster") ](https://www.youtube.com/watch?v=KXJSjte_OAI)

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [January 20, 2021, 4:27pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/4 "2021-01-20T16:27:00Z")

</div>

Thinking is just blowing my mind 😍 🤩  
Is that more efficient?

```auto
function getIndexToIns(arr, num) {
  var count = 0;
  var cool = 0;
  for(let i = 0; i < arr.length; i++){
    if(arr[i] <= num){
      count += 1;
      if(arr[i] === num){
        cool += 1;
      }
    }
  }
  if(cool === 0){
    return count;
  } else{
    return count-cool;
  }
}

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 4:28pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/5 "2021-01-20T16:28:15Z")

</div>

I think you can still tighten up the code even more. You don’t need `cool` or a nested `if` if you change your `if` condition.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 20, 2021, 4:28pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/6 "2021-01-20T16:28:59Z")

</div>

> [@Plabon\_Kumer\_Sarker](#):
>
> ```auto
> if(cool === 0){
> return count;
> } else{
> return count-cool;
> }
> 
> ```

the fact that you remove `cool` it may tell you that you actually don’t need it, and need to make a different adjustment

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 4:32pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/7 "2021-01-20T16:32:42Z")

</div>

Right, I was able to solve this with one variable, one loop, and one `if`, along the lines of:

```auto
let count = 0;
for // yada yada yada
  if (/* some condition */) 
    // some action

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 4:38pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/8 "2021-01-20T16:38:27Z")

</div>

Mildly related - Is there a performance impact/general preference between using a `reduce` vs a `filter` + `length` in JavaScript?

I know in some languages both approaches provide the same performance (Rust) while other languages produce less efficient code for `filter` + `length` (which is a shame because it looks cleaner/more readable).

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 4:45pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/9 "2021-01-20T16:45:45Z")

</div>

I wouldn’t expect a difference, but it also may be engine dependent because I think the spec just specifies the behavior, not the implementation. And JS isn’t exactly a fast language anyway so I think most of the time you don’t worry micro-optimizations like this. Just my $.02.

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [January 20, 2021, 4:46pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/10 "2021-01-20T16:46:31Z")

</div>

Okay 🤗

```auto
function getIndexToIns(arr, num) {
  let count = 0;
  for(let i = 0; i < arr.length; i++){
    if(arr[i] < num){
      count += 1;
    }
  }
return count;
}

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 4:55pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/11 "2021-01-20T16:55:02Z")

</div>

There you go! If you are feeling up for a challenge, you can try to formulate that same logic using high order methods, with either a `reduce` or a `filter` + `length`.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 5:12pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/12 "2021-01-20T17:12:07Z")

</div>

Yeah, I think _reduce_ is the “sexiest” solution. But the _filter_/length solution is good to figure out.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 5:18pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/13 "2021-01-20T17:18:23Z")

</div>

I think it depends upon the community which is sexier?

Generally Rustations prefer

```auto
let arr = [40, 60];
let num = 50;
let index = arr.iter().filter(|x| x < num).count();

```

so that’s where my training/mind is at.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 5:25pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/14 "2021-01-20T17:25:04Z")

</div>

Yeah, I mean it is very dependent on language. But to me that is semantically what _reduce_ is for - reducing an array into a single value. I prefer the reduce, but I also wouldn’t kick your solution out of bed for eating crackers.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 5:29pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/15 "2021-01-20T17:29:40Z")

</div>

Yeah, it took me a while to get used to. I started writing those as reduces but soon saw that most codes used filter+count in that community. Weird how languages pick up quirks like that.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [January 20, 2021, 7:50pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/16 "2021-01-20T19:50:33Z")

</div>

I’m a big fan of `.reduce`, but in this case, what could be more sexy than a solution that eliminates the necessity to sort the array first? I’m not sure how to do this purely with `.reduce`, without a chain `.sort().reduce()`. But then again I’m maybe just easily impressed by fancy one-liners like

`return arr.filter(e => e < num).length`.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 20, 2021, 7:55pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/17 "2021-01-20T19:55:00Z")

</div>

Oh, you can reduce with a ternary if you like those

```auto
return arr.reduce((total, elem) => total + (elem < num ? 1 : 0), 0)

```

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [January 20, 2021, 8:02pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/18 "2021-01-20T20:02:46Z")

</div>

That’s so hot. Totally escaped me.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [January 20, 2021, 8:56pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/19 "2021-01-20T20:56:33Z")

</div>

Of course, you could also simplify a tad more by coercing the boolean to a number with the unary plus operator,

```auto
total + (elem < num ? 1 : 0)

```

becomes

```auto
total + +(elem < num)

```

But at some point things start getting unnecessarily arcane, sacrificing readability for cuteness.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [January 20, 2021, 9:41pm UTC](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714/20 "2021-01-20T21:41:40Z")

</div>

The fine balance between impressing and annoying your co-workers and/or future self.

[Next page](https://forum.freecodecamp.org/t/is-my-code-efficient-where-do-i-belong-basic-algorithm-scripting/441714.md?page=2)
