#Typescript mini-challenge question / clarification

2 messages · Page 1 of 1 (latest)

jade breach
#

In this lesson https://v2.scrimba.com/learn-typescript-c0l/~0s the instructor uses a conditional on an enum to determine whether a star appears after the reviewer's name:

import { LoyaltyUser } from './enums'

export function showReviewTotal(value: number, reviewer: string, isLoyalty: LoyaltyUser) {
    const iconDisplay = LoyaltyUser.GOLD_USER ? '⭐' : ''
    reviewTotalDisplay.innerHTML = 'review total ' + value.toString() + '| last reviewed by ' + reviewer + ' ' + iconDisplay
}

logging the value inside the function to check e.g. console.log(LoyaltyUser.SILVER_USER ? LoyaltyUser.SILVER_USER : 'False') etc always logs the string and never evaluates to false regardless of what the user type is or which review the function is being run on.

If you change the display function params to another review such as Omar's or Andrzej's (silver and bronze users respectively), for example
showReviewTotal(reviews.length, reviews[1].name, reviews[1].loyaltyUser) then the star still shows up, even though it should only show for gold users.

Am I missing something here? I just started learning yesterday so that's entirely possible but I can't see how the conditional makes sense or is doing what it's supposed to. TIA

dusky lark
#

When using a ternary the expression to the left of the ? is the "condition" to be evaluated.

In your case that expression is: LoyaltyUser.GOLD_USER

That expression alone will only check for truthyness and since LoyaltyUser.GOLD_USER exists, it is truthy and therefore the first result (the star) is returned.

So... you need to expand that condition a little. You need to check if the current user IS a loyalty user.

In this case you probably are currently missing a few things...

First of all, you have (hopefully) updated the reviews array to include the new enums for the loyaltyUser attribute.

Secondly, you'll need to update the parameters of the showReviewTotal() function such that isLoyalty is no longer a boolean but rather of type LoyaltyUser (the enum).

Finally in your ternary you should be checking the condition of isLoyalty === LoyaltyUser.GOLD_USER instead of simply LoyaltyUser.GOLD_USER

If you need more help kindly post a forked version of the scrim so that I can see where you've gotten in your code (the link you posted is for the lesson proper and doesn't show what you've coded).