Basic Algorithm Scripting - Where do I Belong

Tell us what’s happening:
Code is doing what the challenges asks, but I think this is not the way the challenge is supposed to be tackled.

Am I missing something very basic or is my thinking somehow flawed as it didn’t even cross my mind that I don’t have to find the highest number to sort the array?
Also why does the first if else need i + 1 instead of just i?

It feels kind of far fetched to assume that people who have just picked up coding would be writing super concentrated code as shown in solutions.
Seeing all the simplified and neat short bits of code as solution makes me feel extremely stupid as my code is always like a wall of text compared to the solution and it always makes me feel really disappointed in myself.

Your code so far

function getIndexToIns(arr, num) {
  let sorted = arr.splice(arr.indexOf(Math.max(...arr)), 1);
  while (arr.length != 0) {
    arr.splice(arr.indexOf(Math.max(...arr)), 1);
  for (let i = 0; i < sorted.length; i++) {
    if (num == sorted[i]) {
      return i;
    } else if (num > sorted[i] && num < sorted[i + 1]) {
      return i + 1;
    } else if (num > sorted[sorted.length - 1]) {
      return sorted.length;
  return 0;

console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Algorithm Scripting - Where do I Belong

Link to the challenge:[spoiler]This text will be blurred[/spoiler]

Nobody expects that.

Writing code like that takes a lot of practice.

Why are you destroying arr here?

Your code is unique for sure. Most of the algorithms you see, just sort the array and the iterate through until the index is found where the number would be inserted. Your code takes a round about way of achieving instead of just using the built-in sort() method.

I would not feel bad about your solution. You accomplished the task and now you can look at the other solutions for ideas about different ways of approaching the same problem. The important of solving this challenge with your own code is that you created your own solution without using someone else’s solution as a basis for your own. You should be proud to know you CAN figure things out.

I will say 90% of the battle of solving a problem is developing an algorithm to solve the problem. The other 10% is actually writing the code for the algorithm developed. These challenges are about getting you thinking about algorithm steps and then connecting programming syntax/concept to the algorithm.

I’m removing the highest number from arr so there is a new highest number to unshift into sorted and also to meet the while loops requirement so I’m not stuck in a infinite loop.

Sure, but you are doing extra work here.

Two good questions to ask yourself for these problems

  1. What is the least work I can make the computer do here?

  2. Do I really need to destroy that input?

You create a copy, destroy that copy, destroy the input, and create a new sorted copy. That’s a lot of work.

Can you do that with less work? Can you do it without destroying the input?

Getting a problem solved is hard. Thinking of ways to improve the code can be just as hard.

I don’t quite follow.
I create a new array called sorted with the highest number of arr. Then I unshift the new highest number of arr into sorted and then delete the highest number from arr.
At what point do I destroy the copy I created?

Woops. I thought you were using shift.

Don’t get too buried in the weeds though. My point still stands that this is more work than you need. Destroying one array and building another one element at a time isn’t very effecient.

Yeah, it clearly is more work than necessary as my code is around 20 lines long instead of around 5 that could solve the challenge.

Am I supposed to just keep searching through the internet for a ready built method that I have no way of knowing if it even exists?

What I’m trying to say is that if I’m not told about something existing it can be pretty hard to find something like that.
It’s easy for me to find Bernoulli’s principle because I know it exists.

This challenge really found a way to get under my skin and release the built-up frustration of underperforming in the previous challenges.


That’s how programming works though. You wanted to sort the array. You made one way to do it. That’s great. There are many ways to accomplish every non-trivial coding task. You get something working and eventually you replace it with something better.

If you wrote code that accomplished the challenge - you did not underperform at all. The #1 priority is functional code.

Writing simple code is hard and takes practice. Writing clear code is hard and takes practice. Writing maintainable code is hard and takes practice. Writing efficient code is hard and takes practice.

The first step is getting the code working. The rest comes with practice as you write, read, and edit more code.

Thank you very very much for all the encouraging words. I really appreciate your input.

1 Like