Naive style of coding

Hello FreeCodeCampers,

According to
my code:

function largestOfFour(arr) {
// You can do this!
let finalArray = [];

let sortedArray;

for( let i = 0; i < arr.length; i++ ){
sortedArray = arr[i].sort((a,b) => {
return b-a;

return finalArray;

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

is very naive and not close to the provided answers. As you can see, I needed to console.log several variables to make sure what are their values (commented out).
The last answer is just impossible to achieve currently.

How is it possible to learn writing more clean code? Should I learn provided answers and repeat them until having ability to write them by myself? Many thanks in advance!

Practice. Learn to recognise what’s wrong in your code and also to use Read-Search-Ask method. Keep coding.

Try to solve the challenges on your own, break down the steps needed (just the logic), then read on methods and operators you could use or write your own functions, research on how to implement your steps if you don’t know something that could do it, ask for help if you get stuck. And then, once you are able to solve it, you can try to understand other solutions or wait at least a few days and try to solve it again, it will be a completely different solution because you will have learned many new things.

Or just keep going, when you feel ready start ready the forums on challenges you have already solved to see many different ways to solve them, and help someone else if you get stuck.

Eventually you will be able to solve this with one line of code inside your function, but to get there you need practice and learning to use new things. Keep coding.

1 Like

Thank you, I appreciate your comment.

Hey bartek. I agree with leah but to add on… I think you’ve done a great job console logging! I’m a beginner myself albeit a little further on my coding than you but I started the same way - creating too many for loops when it can be done with an array method or two. But even now, I still console log here and there to make sure a function is going to work as intended. I don’t think it’s a bad thing. If you think about it, you could be helping save some time from debugging by preemptively console logging. :grin: Once you start doing more challenging algorithms, I think it will click faster what you need to do and it is so satisfying. If you want to challenge yourself early, I recommend sites like Codewars to start practicing. :blush: I just looked at my old code for this same challenge and it is pretty much the same as yours. Haha. Good luck to you!

1 Like

Actually your code passed all tests for me. Maybe you need to refresh your browser?

Don’t sell yourself short. I think you are on the right track and your logged values are very much the way to go. You are clearly seeking to understand what your code does to find a solution. That is solid problem solving at work.

As time passes you will learn about javascript features that will make your solutions more elegant, shorter, and (sometimes) faster. These are good things to know but none are a substitute for problem solving skills. Better to apply a “naive” solution that you understand fully than one that you know works but are a little fuzzy on why it works.

Walking through this particular solution you did exactly what everyone else did. When you get to functional programming section you will see that your solution is not all that different than those others you’ve seen.

  • You looped through the array.
  • Determined largest value for each subarray*
  • You saved each largest value into a result array to be returned.

*Your solution avoids the single most common pitfall on this challenge. Frequently campers start by comparing to zero assuming that the largest value will larger than zero.

My guess is that you’ve seen sweet one line solutions using array methods. You’ll be learning those soon enough in future FCC sections. Having an understanding of basic “naive” programming control structures like for…loops will serve you well when you get there.

1 Like

Thanks! It passed: hopefully the quality will be better in time :slight_smile:

Thank you Cara :slight_smile:

1 Like