Question about how should I write code

Tell us what’s happening:
Describe your issue in detail here.
Hello, there. I am a beginner, my code don`t have any trouble passing the run test.

The thing is when I read the solutions in the get help section, all the solutions are using nested methods and functions, trying to keep the code really short and clean.
Should I try to avoide writing code like am doing it right now? focuse on trying to keep my code really short. Or will it come naturally when I am more experienced with javascript??

Thanks.

  **Your code so far**

function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let targetKeyNum = Object.keys(source).length;
let keys = Object.keys(source);
let vals = Object.values(source);

// Number of keys match
function matchingTimes(testList) {
  let keyMatchTimes = 0;
  let dList = Object.keys(testList);
  for (let b = 0; b < targetKeyNum; b++) {
    let testKey = keys[b];
    if (dList.includes(testKey)) {
      keyMatchTimes += 1;
    }
  }
  return (keyMatchTimes == targetKeyNum) ? true : false;
}

// Number of values match
function valueMatch(item) {
  let valMatchTimes = 0;

  for (let i = 0; i < targetKeyNum; i++) {
      if (item[keys[i]] == vals[i]) {
        valMatchTimes += 1;
      }    
  }
  return (valMatchTimes == targetKeyNum) ? true : false;
}


let keyMatch = collection.filter(item => matchingTimes(item));
let valMatch = keyMatch.filter(item => valueMatch(item));
arr = valMatch;

// Only change code above this line
return arr;
}



whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });



  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 OPR/77.0.4054.277

Challenge: Wherefore art thou

Link to the challenge:

Hi @bananahead !

Focus on solving the problems first.

Then you can always revisit it later to refactor if you want.

With time and practice you will learn how to write shorter code.
So don’t worry about that now.

Also, remember that shorter does not always mean better.

3 Likes

ofc you should try to improve your code writing skills, but what you know now limits what you can produce and only over time and practice and acquiring new skills, your code will naturally take more shorter and clean shape. Everyone in their beginning(and even later) wrote longer code and failed to utilize some advanced methods.

The solutions(usually ones that have been upvoted) are often wrote by people far surpassing the skills stage of the respective challenge and can often look foreign and using methods not corresponding with the challenge background, so dont feel bad when your code does not resemble them. I recall how much it took me to solve certain challenges and how wide and rusty code i wrote in order to do so and every once in a while i take on an old challenge(when people call for help on them, here on the forums) and having acquired mcuh more experience i can compare how much my current code differs from before.

2 Likes

Thank you, that clears out my confustion.

Thanks man, I appreciate it.

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