Javascript challenge 92

Hello guys ! Completely stuck at this level ; completely new with JS.
Don’t understand why it’s not working. I might be of course completely wrong with the syntax

Blockquote

// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
  return records;


if (value === '') delete records[id][prop];
  else if (prop === 'tracks') || (prop === 'artist') || (prop === 'albumTitle'){
    records[id][prop] = records[id][prop] || []; }// this is called shortcircuit evaluation, see below for explanation
    records[id][prop].push(value);
  } 
  return records;

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(recordCollection, 1245, 'tracks', 'Addicted to Love');
updateRecords(recordCollection, 1245, 'albumTitle', 'Riptide');

If someone would explain me, I would appreciate. Have a nice code. Thks Beatrice

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Several things jump out at me:

This function wouldn’t actually do anything because it has a return statement as the first line. Remember that a return exits the function.


This comment makes me think that your code is copied and pasted from someone else’s solution, rather than something you wrote.


Also, it looks like you are missing at least one }.

1 Like
// Setup
var recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(records, id, prop, value) {
  //Ariel pointed this out, but nothing can happen
  //after a return in a function 
  return records; 


if (value === '') delete records[id][prop];

//the else if is wrapped incorrectly causing a 
//syntax error
  else if (prop === 'tracks') || (prop === 'artist') || (prop === 'albumTitle'){

// this is called shortcircuit evaluation, see below for explanation
    records[id][prop] = records[id][prop] || []; 
//In the case of || short circuiting the evaluation stops
//once a truthy value is hit or it continues all
//the way to the end if non are present
    }
    
    
    records[id][prop].push(value);
  } 
  //this return is another syntax error a it is outside 
  //a function
  return records;

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(recordCollection, 1245, 'tracks', 'Addicted to Love');
updateRecords(recordCollection, 1245, 'albumTitle', 'Riptide');

I commented out your code to explain some thing that are wrong with it.

Hi @Beatrice !

It looks like you tried to copy solution 2 from the guide but you copied it incorrectly.

I would advise you not to look at answers until after you have solved it on your own first.

My advice would be to reset the lesson and work on coming up with your own solution.
You will learn a lot more that way. :grinning:

Then we can help you with your solution so you can understand the problem better.

1 Like

Hello ! Sorry for replying late. But thanks for the information. Grateful. B.

Hi @jwilkins.oboe ! Well I am discovered. I’d have to hang on it. did 2 lines correct but the whole thing is still hard.

if (prop !="tracks" && value !=""){
    console.log(records[id][prop]=value)
  }

this works. so I will go on. Thks very much.

Thank you very much this is very clear ! Best regards and keep enjoying code ! Beatrice

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