What are the characteristics of writing code without if in JavaScript
This article mainly explains "what are the characteristics of writing code without if". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the characteristics of writing code without if"?
Example 1: counting odd numbers in an array
Suppose we have an array of integers arrayOfIntegers, and now we need to count the odd numbers in it:
Const arrayOfIntegers = [1,4,5,9,0,-1,5]
Use if
Let counter = 0; arrayOfIntegers.forEach ((integer) = > {const remainder = Math.abs (integer% 2); if (remainder = 1) {counter++;}}); console.log (counter)
Console.log (counter)
No need for if
Let counter = 0; arrayOfIntegers.forEach ((integer) = > {const remainder = Math.abs (integer% 2); counter + = remainder;}); console.log (counter)
Without if, we skillfully take advantage of the characteristics of odd and even numbers, where the remainder divided by 2 is 0 and 1, respectively.
Example 2: judge weekdays and weekends
Given a date (such as new Date ()), determine whether it is a weekday or a weekend, and return "weekend" and "weekday" respectively.
Use if
Const weekendOrWeekday = (inputDate) = > {const day = inputDate.getDay (); if (day = 0 | | day = 6) {return 'weekend';} return' weekday'; / / Or, for ternary fans: / / return (day = 0 | day = 6)? 'weekend':' weekday';}; console.log (weekendOrWeekday (new Date ()
No need for if
Const weekendOrWeekday = (inputDate) = > {const day = inputDate.getDay (); return weekendOrWeekday.labels [day] | | weekendOrWeekday.labels ['default'];}; weekendOrWeekday.labels = {0:' weekend', 6: 'weekend', default:' weekday'}; console.log (weekendOrWeekday (new Date ()
Do you find that there is actually some information in the if sentence? It tells us which day is the weekend and which is the working day. So, to get rid of the if statement, we just need to write this information to the weekendOrWeekday.labels object and use it directly.
Example 3: doubler function
Write a doubler function that does different operations depending on the type of parameter:
If the parameter is a number, multiply by 2 (i.e. 5 = > 10,-10 = >-20)
If the parameter is a string, each character is repeated twice (i.e. 'hello' = >' hheelloo')
If the argument is a function, call it twice
If the argument is an array, call the doubler function with each element as a parameter
If the parameter is an object, call the doubler function with each attribute value as the parameter
Use switch
Const doubler = (input) = > {switch (typeof input) {case 'number': return input + input; case' string': return input .split (') .map ((letter) = > letter + letter) .join ('); case 'object': Object.keys (input) .map ((key) = > (input [key] = doubler (input [key]) Return input; case 'function': input (); input ();}}; console.log (doubler (- 10)); console.log (doubler (' hey')); console.log (doubler ([5, 'hello'])); console.log (doubler ({a: 5, b:' hello'})); console.log (function () {console.log ('call-me');}),)
No need for switch
Const doubler = (input) = > {return doubler.operationsByType [typeof input] (input);}; doubler.operationsByType = {number: (input) = > input + input, string: (input) = > input .split (') .map ((letter) = > letter + letter) .join ('), function: (input) = > {input (); input () }, object: (input) = > {Object.keys (input) .map ((key) = > (input [key] = doubler (input [key]); return input;},}
You know, I bind the operation corresponding to each parameter type to doubler.operationsByType, so that I don't need a switch statement to implement the doubler function.
Thank you for your reading, the above is the content of "what are the characteristics of writing code without if". After the study of this article, I believe you have a deeper understanding of the characteristics of code without if, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!