= 1 ) { // use masculine tense // append singular or plural ending word += ( totalCount == 1 ) ? "us" : "i"; } else { // use feminine tense // append singular or plural ending word += ( totalCount == 1 ) ? "a" : "ae"; } var report = word + " attended: " + totalCount; console.log( report ); 3 ternary.html"> = 1 ) { // use masculine tense // append singular or plural ending word += ( totalCount == 1 ) ? "us" : "i"; } else { // use feminine tense // append singular or plural ending word += ( totalCount == 1 ) ? "a" : "ae"; } var report = word + " attended: " + totalCount; console.log( report ); 3 ternary.html">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ternary Operators © Copyright 2014, Fred McClurg All Rights Reserved.

Similar presentations


Presentation on theme: "Ternary Operators © Copyright 2014, Fred McClurg All Rights Reserved."— Presentation transcript:

1 Ternary Operators © Copyright 2014, Fred McClurg All Rights Reserved

2 Ternary Operator: “if” Shortcut Description: Abbreviated notation of the “if” condition. Often used on one line. Used for returning a value upon a true or false condition. Syntax: condition ? trueValue : elseValue ; 2

3 Simple Ternary Operator var word = "Alumn"; var totalCount = 4; var maleCount = 1; if ( maleCount >= 1 ) { // use masculine tense // append singular or plural ending word += ( totalCount == 1 ) ? "us" : "i"; } else { // use feminine tense // append singular or plural ending word += ( totalCount == 1 ) ? "a" : "ae"; } var report = word + " attended: " + totalCount; console.log( report ); 3 ternary.html

4 Ternary Operator: Student Exercise Problem: Create a script that uses the Ternary Operator to make the following words plural or singular based on the number of each as specified by the zoo object. 4 Note: The plural of the words in the table end with an “i” and the singular of those words end with “us”.

5 Ternary Solution: One var zoo = {}; // declare object // initialize object values zoo["hippopotam"] = 3; zoo["cact"] = 2; zoo["octop"] = 1; for ( var key in zoo ) { var noun = key; var count = zoo[key]; noun += ( count != 1 ) ? "i" : "us"; var quote = "Zoo has " + count + " " + noun; console.log( quote ); } 5 ternaryObj01.html

6 Ternary Solution: Two // declare and initialize object var zoo = { hippopotam: 3, cact: 2, octop: 1, }; for ( var key in zoo ) { var noun = key; var count = zoo[key]; noun += ( count != 1 ) ? "i" : "us"; var quote = "Zoo has " + count + " " + noun; console.log( quote ); } 6 ternaryObj02.html


Download ppt "Ternary Operators © Copyright 2014, Fred McClurg All Rights Reserved."

Similar presentations


Ads by Google