• Frustration - Start Over or Bang Head

 

I'm working on just the very first code challenge and maybe I need to give up and accept that I can't figure it out. But, wait... everyone says programming is easy, just practice and practice. Okay, I'm practicing.

I am working on manipulating an array passed in as a parameter to a JS function, so I created a forEach loop and it seems I made a complete mess of things.

Do you ever just throw hours and hours of code away to try a different approach? Maybe, I should be using a normal 'for loop' instead of a 'forEach'. Here's my code I've worked and worked for hours. Welcome to my mess! Should I just throw it away and start over or work with it for many more hours?

Seriously, though, my question is for experienced programmers. DO YOU EVER JUST THROW IT AWAY OR KEEP HACKING AT IT?

// create myResult to return correct data
var myResult = [];
console.log('Function parameter data: ' + data);

// begin loop of function (data) parameter collection
data.forEach
(
    function(element) {

// Search (data) array element for match
    var goodPos = data.indexOf(element, 2);
    console.log(data+' : '+goodPos);

// goodPos less than 0 means no match found
    if (goodPos > 0) {

// Store the results in correct order
            myResult.push(data[goodPos]);
        }
    }
)
// console.log ('My Result: ' + myResult);
return myResult;