# Basic Algorithms - Getting through them but not in the best way

Basic Algorithms - Getting through them but not in the best way
0

#1

I’m getting through the Basic Algorithm Scripting challenges at a reasonable pace, but I’m worried that I’m not getting as much as I could out of them as my answers vary significantly from those found on GitHub.

For example, see my answer to “Where Do I Belong” vs the one shown here:

mine:

``````function getIndexToIns(arr, num) {

function compareNumbers(a, b) {
return a - b;
}
var sorted = arr.sort();

for(var i=0; i <sorted.length; i++) {
if (num > Math.max(...sorted)) {
return sorted.length;
} else if (num <= Math.min(...arr)) {
return 0;
} else if(num === sorted[i]){
return sorted.indexOf(sorted[i]);
}else if (num >= sorted[i] && num <= sorted[i+1]) {
return sorted.indexOf(sorted[i])+1;
}
}

}
getIndexToIns([2, 5, 10], 15);
``````

Suggested:

``````function getIndexToIns(arr, num) {
arr.sort(function(a, b) {
return a - b;
});

for (var a = 0; a < arr.length; a++) {
if (arr[a] >= num)
return parseInt(a);
}

return arr.length;
}
``````

Clearly the second version is much more efficient and mine is the product of a lot of guess work - is this something I should be concerned about? How can I improve my solutions?

#3

@james_b
Hi James,
This is a common worry. Your code is not as concise as the sample solution true. But it works I guess? And passed the challenges.
If you have the time to do everything that P1xt suggests, fine. However, I believe as you go along the process you will learn more efficient ways to code so I personally wouldn’t recommend re-doing everything.
It is good to read the suggested solutions and try to understand them. I have found many of them lack any explanation. Remember in the real world the pressure is to get something that works rather than the most efficient code possible.
Having said that, they will recommend further reading on functions you may not have seen yet so always follow up any hints.
If you are unsure or stuck it is always worth popping into chat for suggestions as to how to improve things.

#4

Don’t worry! I’m on to the intermediate challenges now and some of my solutions have been UGLY. But at the moment, I’m just happy to get through them, & I still feel like I’m learning a lot and getting good mental training.

#6

That’s reassuring - I’m only a few weeks into learning JavaScript with very little programming experience, so I’ll stop worrying.

Thanks for the YDKJS tip too - looks like that series could be very useful.

#7

Yeah it’s my first shot at real programming - I’ve cut and pasted bits of PHP for Drupal sites before but never really understood what I was doing. I think I need to work on my patience a little - I keep reaching for the answers when I get frustrated.

#8

I totally agree with @Mozar10 that if you keep going, when you revisit these challenges, you’ll see multiple/cleaner solutions. In the meantime, something that has been instrumental in helping me see what my code is doing (line-by-line) is repl.it. I try out my solutions there first, sometimes coming up with more than one–then I copy-paste into FCC. This way, I can console.log variables or outputs of functions, etc. to see if it actually does what I think it does. This has led to a deeper understanding than doing the solution in the FCC environment. I also agree that looking at solutions isn’t helpful–it gets the job done, but is it leading to a better grasp? Maybe, but maybe not. If you check solutions then research what the solution did so that you understand it step-by-step, it could be helpful. What’s worked well for me is (1) reading documentation (esp. MDN) and examples and (2) using repl.it for trial-and-error. Happy coding!

#9

I never knew about repl.it - pretty handy and not frustrating to use like Chrome console.

Thanks

#10

I know how you feel…ughh…I’m still using FOR loops all over the place and when I see those simple damn solutions I just smack myself…try to make mine better…and move on.

#11

Which YDKJS book covers algorithms - it’s not clear from the Table of Contents?

#13

Thanks, I appreciate the response! Great suggestion.