Record collection: canonical answer does not work.
See original GitHub issueRecord 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)
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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;}
@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:
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.