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?

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.