Combine two functions into one

There are two functions that are called when you click on the buttons, these functions work as they should. You need to make the input event so that the data is immediately output to the page, if I can copy the logic of the first function, then I can no longer make the second function. Let's say I enter 12 in the input, I will output words that are longer than 12, then I go to erase and write en, which should output words that have en - it no longer works here. How can these two functions be made one and the same? hang on the input{[1] event]}

Author: markelov, 2020-10-14

1 answers

Let's say f is your function, which will combine the functions secondButton and firstButton. The difference in their execution lies in the type of the argument, i.e. by entering 12 you are looking for the length, and by entering 'en' you are looking for a specific match in the substring. The difference is only in the types of the passed argument. But, there is one caveat. Your values are taken from the input, which already makes all types strings, and checking through typeof value will not help. There are several solutions to this problem.

  1. Make a checkbox / selector with text on the type "Search by length" and "Search by string". Next, we just need to take the value from the checkbox/slider and process the functionality in different ways
  2. Convert the value from the input using Number (value) and then do a check for the NaN type. If it is NaN, then we will search for a substring, and if the conversion is successful, then we will search by length. I do not recommend the second method, because it is very crutchy and will be incomprehensible to the user
 1
Author: Alexander Chernykh, 2020-10-14 14:05:54