![]() ![]() Whether or not you use ternary operators, it’s important to know what they look like and how to read them in case you encounter them.Description of the problem: I can’t load my experiment to Pavlovia (stuck on “initialising the experiment”), and found the source of the issue in a JS code component, but I can’t figure out why it’s causing an issue. Some people like ternary operators because they save space when working with simple conditional statements, but not everyone likes them or uses them. const num = 4 const positiveCheck = num >= 0 ? "Positive" : "Negative" console.log(positiveCheck) Then, we can console log the value of that variable. ![]() We can store the result of the ternary operator into a variable called positiveCheck. If it's falsy, we'll want to console log "Negative", so that's the second expression, and it comes after the colon. If it's truthy, we'll want to console log "Positive", so that's the first expression after the ?. Using a ternary operator, that will go on the left side of the question mark ?. The condition is num >=0, which means that's what we're checking. ![]() If…else statementsĪn if…else statement is structured like the following: if (condition) When working with conditional statements, it’s important to keep in mind what you’re checking and what order those things should be checked in. Your friend doesn’t want you to buy chocolate ice cream if cookies and cream is an option. ![]() It’s also important to note that the order matters in these statements. Otherwise, if it has cookies and cream ice cream: buy that.Įach one of these statements has a condition (“the store has mint chocolate chip ice cream”) and a statement to execute if that condition is true (“buy that”).If the store has mint chocolate chip ice cream: buy that.If the store doesn’t have that either, just get me chocolate ice cream.” Written another way, your friend is saying: If the store doesn’t have that, please get cookies and cream. They tell you, “If the store has mint chocolate chip ice cream, please get it. Let’s say your friend asks you to pick them up some ice cream from the grocery store. Conditional statements are used all the time in the non-programming world. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |