Copying code, good practice or not?

Hello, I completed a challenge in the intermediate scripting section, with using code from stackoverflow. I learned a new way how to use filter, and the method includes(). Is copying code a good practice in general for beginner, as i learned a new method when copying the code?

my code:

function diffArray(arr1, arr2) {
  

  let result = arr1.filter(word => !arr2.includes(word))
  let result2 = arr2.filter(word => !arr1.includes(word)).concat(result)


  

  return result2

}
  
  diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])

from stackoverflow

var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];

var filteredKeywords = fullWordList.filter((word) => !wordsToRemove.includes(word));

console.log(filteredKeywords);

not really IF you did not try to understand his/her code + you need to do it by yourself first then if you are stuck, get some help.

1 Like

You can learn some really slick stuff by looking at StackOverflow, and learning how to research code questions online is very important.

But, you need to make sure you can fiddle with the code and understand how it works.

I understand the solution. Is it cheating?

Well, the freeCodeCamp Academic Honesty Policy says

"Aside from using open source libraries such as jQuery and Bootstrap, and short snippets of code which are clearly attributed to their original author, 100% of the code in my projects was written by me, or along with another camper with whom I was pair programming in real time.

so, researching is fine in the freeCodeCamp projects so long as you add a comment next to the line of code stating where you got it from. I’d do something like this:

function diffArray(arr1, arr2) {
  // Note: researched array filtering technique on StackOverflow
  //   https://stackoverflow.com/questions/45599749/using-filter-to-compare-two-arrays-and-return-values-that-arent-matched
  let result = arr1.filter(word => !arr2.includes(word));
  let result2 = arr2.filter(word => !arr1.includes(word)).concat(result);

  return result2;
}

Heck, I even do this in my professional code sometimes.

1 Like

I mean it’s a bit weird that it’s ok to copy code, but not according to the FCC Academic Honesty Policy…

Copying small snippits of code for the freeCodeCamp projects is ok if you attribute the source.

What do you think on the learning perspective for a beginner, when copying code? @JeremyLT

I think it’s fine to copy a line or two here or there so long as you play around with the code, modify it, and see how it works.

A lot of consulting documentation for languages and libraries involves looking at sample code and modifying it until it works the way you need it to work.

Very broadly speaking, I think that an important distinction exists between “copying” and “researching”. Are you using someone else’s solution to your problem? Or are you learning new techniques and figuring out how to apply them to your problem?

Searching around for ways to compare arrays and learning about includes() and trying that out in your solution is a pattern that’s pretty common in programming. StackOverflow is huge for a reason, and it’s not just because students want to find homework answers. That said, the fact that you seem to have copied from a StackOverflow answer so completely that you’re using the same non-applicable variable name (words) is a bit of a red flag. When we copy a solution it often makes sense to us when we read it, but it also sort of goes in one ear and out the other. It also doesn’t tend to help with understanding the core concepts at play - just one way to apply them.

My suggestion for doing helpful research instead of finding solutions is to think more about what you’re searching for. Sit down with your pencil and paper and think about how you want to solve the problem at a logical level. Fill in the “code” components that you know (in your own shorthand, not perfectly). Then research the specific gaps in your solution. In this case, for example, maybe something like “how to tell if item is in javascript array” or “javascript array methods”, etc.

When it comes to “cheating” and freeCodeCamp. These challenges aren’t required and the academic honesty policy isn’t very concerned with them. Many of the challenges are fairly small, fundamental, and common. If you go looking for help you’re more likely to find a complete solution (or close to it) because the solutions aren’t very complex. The academic honesty policy relates to the projects and claiming a freeCodeCamp certificate. Your projects will be much more complex, so googling how to find the intersection of two arrays is just going to be one small snippet in there.

1 Like

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