Tell us what’s happening:
I tried using ternary operator to check if currentTitle
and currentArtist
are truthy, thus set it to a different value, else blank if falsey.
Not really sure where the problem lies.
Any tips or advices are greatly appreciated.
Thanks in advance!
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
currentTitle ? playingSong.textContent : "";
currentArtist ? songArtist.textContent : "";
// 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/127.0.0.0 Safari/537.36
Challenge Information:
Learn Basic String and Array Methods by Building a Music Player - Step 70
Hi,
the ternary operator is technically correct:) (edit: it isn’t- sorry, I misread it) you’re deciding between two values and not assigning them to anything right now.
variable = condition ? <value if true> : <value if false>
Hi there
,
I’m a bit confused about condition, since it only requires the value to be truthy. Following your example is this a correct path to fix my code:
currentTitle ? currentTitle = playingSong.textContent : "";
currentArtist ? currentArtist = songArtist.textContent : "";
Sorry- I’ve read it wrong the first time.
In the first case, playingSong.textContent
is the variable you want to set, and the currentTitle
is both the condition you’re checking and one of the possible values.
currentTitle ? a : b
checks if there’s any value assigned to currentTitle
(to be fair, this one is a bit confusing- I’ve only completed it yesterday, and had to figure out what’s actually happening there after completing it)
1 Like
Hi there!
The variable name with textContent property should be placed in the very left side.
Example
variable.property = condition ? ifConditionTrue : otherWiseExicution;
2 Likes
Hi there!
Am I getting closer to the solution now? I’m still a bit confused so a bit in-depth explanation about why is textContent
property should be placed in the very left side would be greatly appreciated!
playingSong.textContent = currentTitle ? playingSong.textContent : "";
songArtist.textContent = currentArtist ? songArtist.textContent : "";
After the question mark ?
remove the variable.textContent
and add that value you are using as condition
.
Because that is correct Syntex to write code for checking the condition using ternary operator .
2 Likes
Hi, I believe that your question has already been answered however, it is best to remember with ternary operators syntax is as follows:
variable = conditional? action if true: action if false.
Varible = the thing you want to change so in this case playingSong.textContent.
conditional = the thing you are checking to be true - currentTitle.
action = update if true to first and then update if false after :
so this ternary is checking if currentTitle is true and then assigning currentTitle to playingSong.textContent or if false is assigning a empty string.
I hope that makes it easy to absorb.
2 Likes
I see. This makes much more sense now. Thank you very much for the details!
1 Like