Complicated solution


#1


I've finished my program and now the code only records exactly my name in the array. But I think I found the most complicated solution ever. There must be a better way to do this... Right?


/*jshint multistr:true*/

var text = "Lorem ipsum dolor sit amet, eos Carlos quando Cristina pericula torquatos in, at quo impetus incorrupte Roberto concludaturque Carli. Vis ne solum eleifend Jose patrioque, Carlos zril offendit Renata facilisis ex nec, quo quas Marcos putant Thais phaedrum et. Ei vel Carlos iudico Carolina adipiscing. Sanctus Canvenire Cu vis, an audiam Thais vidisse ponderum vim.";
var myName = "Carlos";
var hits = [];

for (i=0; i<text.length; i++){
    if(myName[0] === text[i]){
        var check = 0;
        for (j=i; j<i+myName.length; j++){
            check = check+text[j];
        }
        check = check.substring(1,myName.length + 1);
        //console.log(check);
        if(check === myName){
            hits.push(check);
        }
    }
}

if (hits.length === 0){
    console.log("Your name was not found.");
    //console.log(hits);
}else{
    console.log(hits);
}


#2

You'd start of by establishing what you want to do

Then consider what actions it takes to accomplish that correctly, keep in mind what you've got available to you (loops, arrays, strings, numbers..)

You can do as much without writing any code at all. So far it's all concept. So make sure you're convinced that the idea is correct and adds up to what you want, that there's are no unnecessary steps.

Finally map that idea to code. This should be a fairly straight-forward translation assuming the idea is well thought out. You might have trouble implementing some specific action in a straight-forward manner, if so then you've at least identified what's complicated and you can ask about that particular action and argue for why it could be done simpler (in terms of actions) than the code you came up with.

Point is, you don't need to be limited by your knowledge of the language when creating the solution. You can do all that with what's already entirely familiar to you (English, series of actions, math, pictures, whatever you use to normally reason about things)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.