question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Record collection: canonical answer does not work.

See original GitHub issue

Record Collection

https://www.freecodecamp.com/challenges/record-collection#

Issue Description

The solution as posted on the FCC wiki does not work. After trying a number of solutions, I looked at the wiki for help on a particular aspect of the answer which ended up being the same as my own answer. So, I copied both my code and another non-functional answer, reset the code to the problem, pasted and commented out the failed attempts, copied the solution from the wiki, and pasted in the solution on the wiki. None of the aspects of the challenge are credited

Browser Information

  • Safari, Version 9:
  • Operating System: OS X 10.11
  • Mobile, Desktop, or Tablet: Laptop

Your Code (my answer first, then another FCC member)

 if (value !=="")  {
      if ([id][prop] !=="tracks") {
       collection[id][prop] = value;          // this part works
      } else {
       collection[id][prop].push(value);     // no love for this part    
      }
   }
  else {
      delete collection[id][prop];             // this part works
  }

Another answer that didn’t work:

  function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
    collection[id][prop].push(value);
  } else if (value !== ""){
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];
  }

Screenshot (taken straight from the wiki, pasted and ran – with results)

screen shot 2016-06-15 at 9 47 04 pm

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Nikluscommented, Jun 17, 2016

Found a little bug: The code below is wrong but is accepted as a right answer.

function updateRecords(id, prop, value) { if(value === “”){ delete collection[id][prop]; }else if(prop === “tracks”){ collection[id][prop] = []; collection[id][prop].push(value); }else { collection[id][prop] = value;} return collection;}

The correct answer should be the following - which works too. It checks first if the property “tracks” exists (if collection[id][prop] is true) before pushing. If it doesnt exist it creates a new array.

function updateRecords(id, prop, value) { if(value === “”){ delete collection[id][prop]; } else if (prop === “tracks” && collection[id][prop]){ // check if property exists collection[id][prop].push(value); }else if(prop === “tracks”){ collection[id][prop] = []; // initialize a new array before pushing collection[id][prop].push(value); } else { collection[id][prop] = value; } return collection;}

2reactions
ghostcommented, Jun 17, 2016

@hrokr The real issue is your code, it’s trying to push onto a non existent array at a non existent key. If you really want to keep your code like this, you would need something like this:

function updateRecords(id, prop, value) {
  if (value !== ""){
    if(prop !== "tracks"){
      collection[id][prop] = value;
    }else{
      if(!collection[id][prop]){
        collection[id][prop] = [];
      }
      collection[id][prop].push(value);
    }
  }else{
    delete collection[id][prop];
  }

  return collection;
}

Perhaps there is something wrong with the ‘output’ window. The code I have provided passes but the ‘results’ are the same as your screenshot. But verified locally the code returns the expected result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 common mistakes with rel=canonical - Google Developers
5 common mistakes with rel=canonical. bookmark_border. Stay organized with collections Save and categorize content based on your preferences. Dismiss
Read more >
Is there a way to recognise a Java 16 record's canonical ...
Try this static <T extends Record> Constructor<T> canonicalConstructorOfRecord(Class<T> recordClass) throws NoSuchMethodException, ...
Read more >
What is a canonical name (CNAME) and how does it work?
Learn about canonical names, DNS records that indicate a domain name is the nickname or alias for another domain name. See examples and...
Read more >
relationships - Why can't I see related data in my Apex, or why ...
I'm working with sObject records, and trying to access related record information. For example, I might have a structure like this: Account a...
Read more >
CNAME Record - How it Works, Alternatives & Advanced Use ...
A Canonical Name (CNAME) Record is used in the Domain Name System (DNS) to create an alias from one domain name to another...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found