Whenever it is difficult to solve algorithm challenges, I check the output part and start playing around with my code, until it passes all the tests. And while doing this, I really feel like I am shamelessly cheating.
Guys, what do you think - should I stop doing this. After all I am new may be it is better to not to look at outputs and tests before solving the algorithm challenges logically. Also in real world programming do we get provided with outputs for the corresponding inputs. I would really appreciate your feedback. Thank you!

I would like to see some of your code.

If itâ€™s something like this:

function returnSquared(num) {
if (num === 2) return 4;
if (num === 3) return 9;
if (num === 5) return 25;
...
...
}

then I would say itâ€™s cheating.

Thanks for the prompt reply @jenovs. Please see my code below for â€śSpinal Tap Caseâ€ť Algo Intermediate challenge:
indent preformatted text by 4 spaces

function spinalCase(str) {
var strArr=str.split(" ");
var finalStrArr=[];
var indexCaps=[];
strArr.forEach(function(word,k){
var indexCaps=[0];
for(var i=1;i<word.length;i++){
if(word[i]==word[i].toUpperCase() && word[i]!="-"){
console.log("word[i]",word[i],"i",i);
indexCaps.push(i);
}
}
console.log("indexCaps",indexCaps);
indexCaps.forEach(function(i,j){
j=j<indexCaps.length-1?indexCaps[j+1]:word.length;
console.log("i:",i,"j:",j);
finalStrArr.push(word.slice(i,j));
});
});
console.log("out",finalStrArr);

finalStrArr=finalStrArr.map(function(element,k){
console.log(element);
console.log("condition",element!=="" && k===0 && element[0]==element[0].toLowerCase());
if(element!=="" && k===0 && element[0]==element[0].toLowerCase()){
console.log("element1",element);
return element;
}
if(element!=="" && element[0]==element[0].toUpperCase()){
console.log("element1",element);
return element[0].toLowerCase()+element.slice(1);
}
else if(element!==""){
return element;
}
});

finalStrArr=finalStrArr.filter(function(element){
return element!==undefined && element!=="_";
});
console.log("final",finalStrArr);
console.log("----------------------------------------------");

console.log(finalStrArr.join("-"));
return finalStrArr.join("-");
}

spinalCase('thisIsSpinalTap');

You need to use triple backticks to post code to the forum.
See this post for details.

I really donâ€™t understand what youâ€™re saying here. Are you writing something, watching it fail, and changing the code until it passes? Thatâ€™s so normal, Iâ€™m not sure what the alternative could be.

1 Like

^^this. IMO itâ€™s not cheating as long as you learn from it and even then itâ€™s still not cheating; if you arenâ€™t learning why your code tweaks solve the requirements then youâ€™re just selling yourself short.

No, thatâ€™s not cheating

I would recommend you to sign up for http://codewars.com/ and work on challenges there (start with 8kyu and 7kyu sorted by oldest) and after you finish a challenge look how others solved the same challenge (thatâ€™s why I recommend to start with oldest - they will have most solutions). Youâ€™ll quickly learn a lot of tricks how to solve these problems with much less code (my solution for this challenge is one line long )

2 Likes

lol what your doing sounds a lot like debugging. Every programmer worth their salt will debug and test the program to check output. how are you supposed to know if your program works if you donâ€™t look at what it does?

One thing this style of programming takes away from is looking at the edge cases. When your writing code, your writing to pass the tests that you currently seeing, or you came up with. But you will not be able to imagine the edge cases that you didnâ€™t create. You should rather be thinking about what it takes to break your code, and how you can strengthen it. Just a little lesson Iâ€™ve learned going about this whole thing.

1 Like

Yes, I am altering code so that it will give desired output.

Wow I wasnt really expecting so many replies guys. Thank you for all your advices I am glad to know that I am not cheating overall. Thanks guys!

Google Test Driven Design (TDD) and you will find there is an entire methodology to developing applications with such an approach.

2 Likes

Yes this is basically what OP is doing, and what I have always been doing. TDD is what every developer should be doing, that is writing tests and then writing solutions for those tests.

I wouldnâ€™t have made it much past my first â€śHello Worldâ€ť program if I didnâ€™t look at output and try to figure out why things werenâ€™t working.

I expected this to be something like looking up answers in the wiki or asking for help on Stack Overflow, lol.

I am very new to programming, forgive my naivety. I kinda had an Impostor Syndrome for a while, that is why decided to make a post about it. Thanks!

1 Like

Itâ€™s something weâ€™ve all been through or going through! Donâ€™t worry about it. If you need help, just ask or send a message, everyone is happy to help. As per your method, thatâ€™s not cheating, thatâ€™s being smart. Unless you are exploring the infinite possibilities of the universe and you donâ€™t know the output, there will be some kind of hint of the output you would like, so why not write tests about it? Why not fiddle with the code and play around until you find a solution that fits your needs? Thatâ€™s a real methodology, as stated by our campers friends in the comments; Test Driven Development, you develop writing tests for one or more cases and you build around the tests. In TDD you write a test to make it fail, then you write a solution to make it pass, no matter how stupid the solution is, then you write an elegant solution that makes it good.

1 Like

Dude, it is. Let me share my point of view. I always face problems as you. What I want to say : try write your algorithm than can be as simple as possible and can handle all possible test cases. For practice I really advice you www.codeforces.com. Sort the problems by complexity and try to solve them one by one. After solving you can see other coderâ€™s code and you be astonished how short code can be for that problem while you wrote Ă—2, Ă—3 lines more. This will definitely make you better at coding