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) {
sorted.unshift((Math.max(...arr)));
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/107.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Where do I Belong
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.
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?
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.