What ways can you suggest for making group of if...else statements shorter? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What ways can you suggest for making group of if...else statements shorter?

I know switch, but it seems as long as if..else.

17th Oct 2016, 4:42 PM
Suzie
Suzie - avatar
3 Answers
+ 1
Hi. You can use a short form of the if...else statement result = condition ? conditionTrue() : conditionFalse(); Also this statements can be nested into each other but this can lead to the complex statement that is hard to read and maintain. Here is the sample code: function writeChar(c) { document.write(c+'<br>'); } (1 < 2) ? writeChar('Y') : writeChar('N'); // writes 'Y'
17th Oct 2016, 8:01 PM
yugor
0
use switch statement... or one of the thing i learnt in my senior secondary class... create recursive functions to check... but this works only in specific cases.
8th Jul 2017, 8:28 PM
Shivam
Shivam - avatar
0
Use map(object) data structure. For key put condition, and value should be corresponding function. To access function for processing data , get function from map by condition (key or property) and call
16th Aug 2020, 7:28 PM
Rahmataliyev Shohjahon
Rahmataliyev Shohjahon - avatar