Lost on this challenge

So I wrote:

function updateRecords(object, id, prop, value) {
  if(prop != "tracks" && value != ""){
    object[id][prop] = value; 
  }
  else if(prop == "tracks" && object[id].hasOwnProperty("tracks") != true){
    object[id].tracks = value; 
  }
  else if(prop == "tracks" && value != ""){
    object[id].tracks = value; 
  }
  else if(value == ""){
    delete object[id][prop]; 
  }
  return object;
}

What problems are you having? Can you describe what you think is going wrong?

If I remember correctly, tracks should be an array.

use !== instead of !=
The = sign is used for assignment and == is used for equality and === for strict equality

Answer deleted from the forum

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

So whenever presented with a set of conditions, say 1, 2, 3, never just go if (1) else if (2) else if (3) because that often results in unnecessary code and/or unnecessary computing.

Think about this condition:

If value is an empty string, delete the given prop property from the album.

It makes a lot of sense to test this first doesn’t it? none of the other conditions want an empty string, and by testing this first, you eliminate additional checks later.

Now on to this:

If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .

so say you test this with if (props != 'tracks') then you do not need to test if prop is ‘tracks’ later right? The only alternative to this if statement is if props is equal to ‘tracks’

So now we got 3 checks, first we see if value is an empty string, if not we see if props is not ‘tracks’, then else.
in the else you can put an if/else checking to see if the object[id] already has the track property.

Up until now it’s only been optimization, but here, with how you assign/change value to the track property, is where you went wrong.

track is an array, meaning if the property does not exist, you should initialize it as an array, then push value into it, instead of changing it to a string (value)
if the property does exist, you should push value into it, instead of changing it to a string (value)as well.

So as a general rule, start with the condition that is a standalone condition?

I am confused by this line:

create an empty array and add value to it

It says nothing about making the array a property in the object.

Anyways, I changed it to this and it’s still wrong.

function updateRecords(object, id, prop, value) {
  if(value == ""){
    delete object[prop]; 
  } 
  else if(prop != "tracks"){
    object[id][prop] = value; 
  }
  else {
    if(object[id].hasOwnProperty("tracks") != true){
      var prop = []; 
      prop[0] = value; 
    }
    else {
      object[id][prop].push(value); 
    }

  }
  return object;
}

with this object[id].tracks is still undefined, you need to give it a value

It’s a little hard to generalize best practice. But the goal to achieve is: as few checks performed by the computer as possible, in as few lines of code as possible. Then you would want the if conditions to be easy to read and understand.

In this challenge, you see some other conditions requiring that the value string not be empty, and the last condition is about an empty value string. So by checking that first, you eliminate the need to type something like if (conditionA && value!="") again and again later.

You did very well in translating the thought into code, and you have it almost correct. So how do we know they want arrays in the tracks property? You have to read the existing code. Notice in the objects with an existing tracks, the tracks are always arrays.

And there are two syntax errors in your if (object[id].hasOwnProperty("tracks") != true) condition.

First, you want to initialize the tracks property in the corresponding object, like you’ve done elsewhere. Writing var prop=[] sets the prop you were given as a parameter to [], and does not affect any object. As a result the correct object[id].tracks does not exist.

Second, you tried to set the value of an array element before it exists. If you’ve declared an array const arr = [], that array arr doesn’t have a arr[0] yet. You have to push the new element in.

1 Like

Ok but I’m confused on how to initialize a property as an array. None of these worked:

object[id].tracks [];

object[id].tracks.push(value);

object[id].tracks = [value];

object[id].tracks = new Array(value);

this is initializing the property to an array that contains value

You’re missing a symbol between “tracks” and []

I thought this was it:

    if(object[id].hasOwnProperty("tracks") != true){
      object[id].tracks = []; 
      object[id][prop].push(value); 
 
    }

what’s the whole code? what do the tests say?

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

  }
  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

After updateRecords(collection, 2548, “artist”, “”)
artist should not be set.

which of the conditions does that test fall into? check that part of the code

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