How to highlight each element of the array?

  • 0
    You need to search for an element through arr.map (), so that when the user enters a letter that is in the array, it will return all words that contain this letter.

    I started to write something, but somehow it didn't work out:
    function find1(){
           let ask2 = prompt('What are you want?').toLowerCase();
          let result = ['bread','milk','apple', 'juice','banana','peach', 'pineapple','beer','Putin'].map(item => item.result);
           if(ask2 == result.toLowerCase){
           alert(result);
           }
         }
    JavaScript Ezra Richards, Oct 15, 2020

  • 1 Answers
  • 0
    The normal way

    const letter = prompt(`What do you want?`).toLowerCase();

    const array = ['bread','milk','apple', 'juice','banana','peach', 'pineapple','beer','Putin'];
    const result = array.filter((item) => item.toLowerCase().includes(letter));

    alert(result);




    The map method

    const letter = prompt(`What do you want?`).toLowerCase();

    const array = ['bread','milk','apple', 'juice','banana','peach', 'pineapple','beer','Putin'];
    const result = [];

    array.map((item) => {
    if (item.toLowerCase().includes(letter)) {
    result.push(item);
    }
    });

    alert(result);
    Autumn Merritt

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!