Learn Basic String and Array Methods by Building a Music Player - Step 70

Tell us what’s happening:

I have tried a varied number of syntax and task expression but couldn’t pass my code.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

 
 currentTitle = True ? playingSong.textContent = currentTitle : currentTitle ="";
 currentArtist = True ? songArtist.textContent = currentArtist : currentArtist ="";

// User Editable Region

Your browser information:

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

Challenge Information:

Learn Basic String and Array Methods by Building a Music Player - Step 70

1 Like

Instruction was:

Use a ternary operator to check if currentTitle evaluates to a truthy value.

Therefore the “check” which is the “condition” that goes on the left of the question mark should include the currentTitle in it.

A truthy is any value that evaluates to true (even if it is not strictly equal to true)

i believe this is already included in the code! currentTitle = true ?

actually currentTitle = true assigns true to currentTitle

You were supposed to check if currentTitle is a truthy.

Using the ternary operator syntax, i had tried currentTitle ? , as a condition before assignment of the .textcontent. I am not sure if the condition syntax suites to work for checking for truthy, but I couldn’t pass the code with it. however, had used the syntax successfully using the if statement method in tasks other than this.

here’s an example which may help:

1 ? console.log("it's a truthy") : console.log("this will never run");

try this line to see what happens

also you probably need to set the .textContent of currentTitle and currentArtist to empty string (not the element itself)

Couldnt pass updated code below:
currentTitle ? currentTitle = playingSong.textContent : currentTitle.textContent=“”;
currentArtist ? currentArtist = songArtist.textContent : currentArtist.textContent=“”;

you switched these around? (and in the following line)

i had tried both ways actually.

okay, I took another look and here’s what I found:

Use a ternary operator to check if currentTitle evaluates to a truthy value. If it does, set playingSong.textContent to currentTitle . Otherwise, set it to an empty string.

so the first part is use a ternary which you have done to check currentTitle
the 2nd part 'set playingSong.textContent` to means that we should have playingSong.textContent on the left side of the equal sign.
And on the right side we can have the ternary to choose between the variable and the empty string.

So something like this:

let myVariable = isItTrue ? "yes" : "no";

This code would set myVariable to yes or no depending on the check of isItTrue

I’m a bit confused, while though i understand the syntax used in your description, am not sure how to apply it in the case of the task objective. Since i had used currentTitle as a condition followed by the ternary operator ? , and the rest of the syntax, does it mean i will use a second ternary operator ? sign to check again if currentTitle = playingSong.textContent?
updated code below:
currentTitle ? playingSong.textContent = currentTitle : currentTitle =“”;
current Artist ? songArtist.textContent = currentArtist : currentArtist =“”;

Hi @Abycode ,

Let’s put @hbar1st’s expression and yours side by side to see the differences:

myVariable = isItTrue ? "yes" : "no"
currentTitle ? playingSong.textContent = currentTitle : currentTitle =“”

@hbar1st’s expression syntax is like this:

variable_name = condition ? value_if_true : value_if_false

Your syntax is like this:

condition ? variable_name = value_if_true : value_if_true = value_if_false

Can you see the difference?


What @hbar1st 's expression do is:

myVariable = isItTrue ? "yes" : "no"

Use a ternary operator to check if isItTrue evaluates to a truthy value. If it does, set myVariable to "yes". Otherwise, set it to "no".

What the instruction asked is:

Use a ternary operator to check if currentTitle evaluates to a truthy value. If it does, set playingSong.textContent to currentTitle. Otherwise, set it to an empty string.

Hope this help.

2 Likes

Thank you very much, i tried to put that to work but still can’t pass my code.
updated code below:
currentTitle ? playingSong.textContent = currentTitle : playingSong.textContent =“”;
currentArtist ? songArtist.textContent = currentArtist : songArtist.textContent =“”;

Let’s stop using ternary operator for a moment.
Can you write the code for me using if/else statement?

Check if currentTitle evaluates to a truthy value. If it does, set playingSong.textContent to currentTitle. Otherwise, set it to an empty string.

if (currentTitle) {playingSong.textContent = currentTitle}
else {playingSong.textContent =“”;}
if (currentArtist) {songArtist.textContent = currentArtist}
else {songArtist.textContent =“”;}

if (currentTitle) {
    playingSong.textContent = currentTitle;
} else {
   playingSong.textContent = "";
}

Yes, that’s correct.

Now, an if/else statement like this:

if (condition) {
   variable_name = value_if_true;
} else {
   variable_name = value_if_false;
}

is equivalent to this ternary operator:

variable_name = condition ? value_if_true : value_if_false;

Can you turn your if/else statement into an ternary operator?

What is condition, variable_name, value_if_true, value_if_false here?

Note: A special thing about this case is: condition and value_if_true are the same.

6 Likes

In my example, what is the first 2 words shown?
In your try, what is the first word shown?

Mine is let myVariable
Yours is currentTitle

After that in my example I have an equal sign to assign something to myVariable, how about in yours?

In my example, I am changing the value stored in myVariable. In yours, what variable is being changed? Is it the correct one?

These are all correct. You’re getting close.

But what condition is?

And I didn’t see the sign = and condition in your ternary operator

condition was stated to check for the truthy of currentTitle;
condition = currentTitle = true ?