How do I make these 3 very similar functions shorter

arrow function
javascript shorthand if statement
3 functions of dna
activity evidence of evolution worksheet answers
function of dna in points
arrow function vs function
javascript shorthand if , without else
python if statement multiple conditions

Hello so today I was curios how would I code smarter and shorter. here is litle project I did like 2 weeks ago:

 // Get input element
    let filterInput = document.getElementById('filterInput');
    // Add event listener
    filterInput.addEventListener('keyup', filterNames);

    function filterNames(){
      // Get value of input
      let filterValue = document.getElementById('filterInput').value.toUpperCase();

      // Get names ul
      let ul = document.getElementById('names');
      // Get lis from ul
      let li = ul.querySelectorAll('li.collection-item');

      // Loop through collection-item lis
      for(let i = 0;i < li.length;i++){
        let a = li[i].getElementsByTagName('a')[0];
        // If matched
        if(a.innerHTML.toUpperCase().indexOf(filterValue) > -1){
          li[i].style.display = '';
        } else {
          li[i].style.display = 'none';
        }
      }

    }

    function do1(){
      var input = document.getElementById('ipt1').value;
      var a = document.createElement('a');
          a.setAttribute('href', '#');
          a.textContent = input;
      var li = document.createElement('li');
        li.setAttribute('class', 'collection-item');
          a.onclick=function(){
            var div = this.parentElement;
            div.style.display = "none";
      }
      var div = document.getElementById('div1');
        li.appendChild(a);
        div1.appendChild(li)
        if(input===""){
          alert('please fill in information');
          return false;
        }
  
    }

    function do2(){
      var input = document.getElementById('ipt2').value;
      var a = document.createElement('a');
      a.setAttribute('href', '#');
      a.textContent = input;
      var li = document.createElement('li');
      li.setAttribute('class', 'collection-item');
      a.onclick=function(){
        var div = this.parentElement;
        div.style.display = "none";
      }
      var div = document.getElementById('div2');
      li.appendChild(a);
      div2.appendChild(li);
    
    }

     function do3(){
      var input = document.getElementById('ipt3').value;
      var a = document.createElement('a');
      a.setAttribute('href', '#');
      a.textContent = input;
      var li = document.createElement('li');
      li.setAttribute('class', 'collection-item');
      a.onclick=function(){
        var div = this.parentElement;
        div.style.display = "none";
      }
      var div = document.getElementById('div3');
      li.appendChild(a);
      div3.appendChild(li);
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.99.0/css/materialize.css">
  <title>My Contacts</title>
</head>
<div class="container">
    <h1 class="center-align">
      My Contacts
    </h1>
    <input type="text" id="filterInput" placeholder="Search names...">
    <ul id="names" class="collection with-header">
      <li class="collection-header">
        <h5>A</h5> <input class="ipt" type="box" id="ipt1"><button onclick="do1();">click me to add another name</button>
      </li>
   <div id="div1">   
    
   </div>  
      <li class="collection-header">
        <h5>B</h5> <input class="ipt" type="box" id="ipt2"><button onclick="do2();"> click me to add another name</button>
      </li>
    <div id="div2">  

     </div> 
      <li class="collection-header">
        <h5>C</h5> <input class="ipt" type="box" id="ipt3"><button onclick="do3();"> click me to add another name</button>
      </li>
    <div id="div3">  
     </div>
    </ul>
  </div>

Make one function and take in some parameters:

function doTheThing(idA, idB){
  var input = document.getElementById(idA).value;
  var a = document.createElement('a');
  a.setAttribute('href', '#');
  a.textContent = input;
  var li = document.createElement('li');
  li.setAttribute('class', 'collection-item');
  a.onclick=function(){
    var div = this.parentElement;
    div.style.display = "none";
  }
  var div = document.getElementById(idB);
  li.appendChild(a);
  div.appendChild(li);   
}

Also, your variables div1, div2, and div3 are not defined, they are the IDs that you are using. Call them all with var div as shown above.

Matthews' Plant Virology, These control and recognition functions are mainly found in the 5' and 3' B. Economy in the use of genomic nucleic acids Viruses make very efficient use of the Like prokaryote cells, most plant viruses lack introns but there are some readthrough polypeptide that is co-terminal at the amino end with the shorter protein. PROC SQL: Make it a monster using these powerful functions and options, continued 7 comparison. These differences can give your queries unexpected results if you do not account for them. One significant difference between ALL and ANY is the way they deal with the situation in which the subquery returns no Proc SQL; -----


Add a parameter to your function that allows you to pass the bit that differs between each instance.

Here's a working demo where the function addName takes a number which is concatenated with your div/ipt names.

// Get input element
let filterInput = document.getElementById('filterInput');
// Add event listener
filterInput.addEventListener('keyup', filterNames);

function filterNames() {
  // Get value of input
  let filterValue = document.getElementById('filterInput').value.toUpperCase();
  // Get names ul
  let ul = document.getElementById('names');
  // Get lis from ul
  let li = ul.querySelectorAll('li.collection-item');
  // Loop through collection-item lis
  for (let i = 0; i < li.length; i++) {
    let a = li[i].getElementsByTagName('a')[0];
    // If matched
    if (a.innerHTML.toUpperCase().indexOf(filterValue) > -1) {
      li[i].style.display = '';
    } else {
      li[i].style.display = 'none';
    }
  }
}

function addName(num) {
  var input = document.getElementById('ipt' + num).value;
  var a = document.createElement('a');
  a.setAttribute('href', '#');
  a.textContent = input;
  var li = document.createElement('li');
  li.setAttribute('class', 'collection-item');
  a.onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
  if (input === "") {
    alert('please fill in information');
    return false;
  }
  var div = document.getElementById('div' + num);
  li.appendChild(a);
  div.appendChild(li)
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.99.0/css/materialize.css">
  <title>My Contacts</title>
</head>
<div class="container">
  <h1 class="center-align">
    My Contacts
  </h1>
  <input type="text" id="filterInput" placeholder="Search names...">
  <ul id="names" class="collection with-header">
    <li class="collection-header">
      <h5>A</h5> <input class="ipt" type="box" id="ipt1"><button onclick="addName(1);">click me to add another name</button>
    </li>
    <div id="div1">

    </div>
    <li class="collection-header">
      <h5>B</h5> <input class="ipt" type="box" id="ipt2"><button onclick="addName(2);"> click me to add another name</button>
    </li>
    <div id="div2">

    </div>
    <li class="collection-header">
      <h5>C</h5> <input class="ipt" type="box" id="ipt3"><button onclick="addName(3);"> click me to add another name</button>
    </li>
    <div id="div3">
    </div>
  </ul>
</div>

The Universal Cyclopaedia, Choristers' surplices are generally made shorter and less full, *. A stole is also worn at baptisms, marriages, and similar functions, but not, according to 3 inches wide, sometimes widened at the ends to make room for embroidered crosses. These notes contain many sample calculations. It is important to do these yourself—type them in at your keyboard and see what happens on your screen—to get the feel of working in R. Exercises in the middle of a section should be done immediately when you get to them, and make sure you have them right before moving on.


Below is an example of a few things you can do to simply shorten the code itself.

Also, I haven't read over all of your code, but your use let is fairly lenient so to speak. You should be using const in some scenarios.

// you can remove most semi-colons for starters
const filterInput = document.getElementById('filterInput')

// no need for event listener, unless you plan on using the event more than once
filterInput.onkeyup = () => {

  // can reuse global variable which is declared above
  let filterValue = filterInput.value.toUpperCase()

  // here you can combine the selectors into one variable
  let li = document.getElementById('names').querySelectorAll('li.collection-item')

  // let is not necessarily necessary
  for (i = 0; i < li.length; i++) {
    let a = li[i].getElementsByTagName('a')[0]

    // can use ternary operator instead of if/else statement
    li[i].style.display = a.innerHTML.toUpperCase().indexOf(filterValue) > -1 ? '' : 'none'
  }
}

...

Child's Conception Of Geometry, EAN (7; 3) is transitional between level IIB and stage III. He compares two —-​Doesn't it make a difference that the steps aren't all the same ?—Oh yes, that's  Start studying 3 CORE FUNCTIONS OF PUBLIC HEALTH. Learn vocabulary, terms, and more with flashcards, games, and other study tools.


When you're doing this, the first place to start is by parameterizing the differences. A very quick and dirty way to do this would be something like this:

function doit(index, nonEmptyInput) {
    // assumption: code calling this function will not use an invalid/null value for index
    var input = document.getElementById('ipt'+index).value;
    if (nonEmptyInput && input === "") {
        alert('please fill in information');
        return false;
    }
    // input is valid
    var a = document.createElement('a');
    a.setAttribute('href', '#');
    a.textContent = input;
    a.onclick = function() {
        this.parentElement.style.display = "none";
    }
    var li = document.createElement('li');
    li.setAttribute('class', 'collection-item');
    li.appendChild(a);
    document.getElementById('div'+index).appendChild(li);
}

Cost-Effectiveness in Health and Medicine, Regardless of how the natural history component of the model is developed (i.e., “It is often useful to analyze the data using several time horizons: a short-term during the treatment phase and continues at the same level; or (iii) diminishes in the Figure 5.1 illustrates three possible assumptions the analyst can make to​  Biology: Chapter 3. STUDY. Flashcards. these functions are carried out in the cytoplasm. very similar in sequence from one organism to another. means that


The Cultivator & Country Gentleman, The nucleus of a paramoecium is very similar is yellowish-brown, the feet and under measuring 433 inches from nose to root of tail; the tail is short, measuring but little are arranged triserially, there being three series of cusps across the teeth. Along the edges of its body one can quite easily make out a row of fine hairs  Functions.MyFunction(1, 2, 3) The expression returns the value set by the Set return value action in the function call. Functions which return a value will also appear in the Expressions dictionary , also show up in autocomplete, and also show call tips when entering parameters, just like other expressions.


25+ JavaScript Shorthand Coding Techniques, Become a faster developer and check out this guide to shorthand JavaScript we can simply use a short-circuit logical operator and accomplish the same function volume(l, w, h) { if (w === undefined) w = 3; if (h The spread operator, introduced in ES6, has several use cases that make JavaScript code  You also don't know if thats going to stay true, maybe somewhere down the line you need to implement a very similar function and you'll get to call a lot of these functions again. Or work on a similar project and just copy the entire function as is without having to rewrite a bunch of stuff. – semi Apr 25 '13 at 16:17 |


The Complete Concordance to Shakspere: Being a Verbal Index to All , Being a Verbal Index to All the Passages in the Dramatic Works of the Poet Mary 3 but gentle heaven, cut short all. 2 and cut out his way, to find out - .ii. 3 like an executioner, cut off the - iii. 1 she was washed and cut and trimmed - W. 1 my hand cut off, and made a merry.. - v. CYPHER—cypher of a function . These are seen in organisms that are closely related. 1. Give an example of a homologous structure from this activity: Crocodile leg, human arm Human and cat front legs Analogous structures have very different anatomies but similar functions. These are seen in