Replace if-else condition ladder with Objects

preview_player
Показать описание
Sometimes working with if-else ladder or even switch case statement can be nightmare. In the video we discuss a very simple trick to avoid writing bulky and difficult to read/maintain code, simply by using object in JavaScript.

Get Better At TypeScript:

Brush up on Javascript :

Docker volumes
Рекомендации по теме
Комментарии
Автор

I was doing an exercise on code wars and I had to give output "Player 1 wins" or "Player 2 wins" or "Draw" in rock paper scissors. I made 3 ifs with 3 more ifs in each of them.
And I was surprised that somebody made an object as a hard set of rules (rock beats scissors, paper beats rock etc.) and checked the first player input with name and second player input with value, ended up using only one if (){} else{}.

danser_theplayer
Автор

There are actual hashmaps in JavaScript. You should use them instead.

doltramir
Автор

It is misleading to say hash map are the same as dictionary, an has map is a specific implementation of a dictionary. what you are saying is very wrong, performances in terms of memory usage and operations time (add, search, remove, etc) can vary a lot depending on how the hash map is implemented.

rallolollo