# Naive style of coding

Hello FreeCodeCampers,

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

//console.log(arr);
//console.log(arr.length);
//console.log(arr[0]);
let sortedArray;

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

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. 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. 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

Thank you Cara

1 Like