+ 1

how to find the number of letters available in the array using java script?

consider there is a paragraph which is written with the array. and there is a search box and a button to find the result. if we search any letter lets say a, and hit on the button it should display the number of a's available in the paragraph below the search box. this is my interview question and I didn't crack it. Here is the code let textArray = [{ "text": "Infuse your life with action. Don't wait for it to happen. Make it happen. Make your own future. Make your own hope. Make your own love. And whatever your beliefs, honor your creator, not by passively waiting for grace to come down from upon high, but by doing what you can to make grace happen... yourself, right now, right down here on Earth." }, { "text": "I think people who are creative are the luckiest people on earth. I know that there are no shortcuts, but you must keep your faith in something Greater than You, and keep doing what you love. Do what you love, and you will find the way to get it out to the world." }, { "text": "Where there is a will, there is a way. If there is a chance in a million that you can do something, anything, to keep what you want from ending, do it. Pry the door open or, if need be, wedge your foot in that door and keep it open." }] let selectedText; function displayText() { let selectedTextIndex = Math.floor((Math.random() * 3)); selectedText = textArray[selectedTextIndex].text document.getElementById("content-text").innerHTML = selectedText; } function calculateWordCount() { let searchText = document.searchForm.searchText.value; console.log("Text is: ",selectedText) console.log("Search text is: ",searchText) let wordCount = 0; //code goes here document.getElementById("output-text").innerHTML = "Word count is: " + letter_Count; } this is the actual code they gave it to me. I need to update code to get the correct output

21st Sep 2019, 1:48 PM
Gowtham Raj
Gowtham Raj - avatar
5 ответов
+ 5
Write a for loop, checking every digit of that paragraph. Inside it check if letter == "a" You need a var (len, for example), and every time there is an "a" do len++ When the for loop ends just, just write the "len" inside a p, h1-6, or anything you want. for input, replace "a" with document.getElementById(id of ur input).value //I am not sure if it would work, because I didn't tried it, but you could try. 😊
21st Sep 2019, 2:10 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 1
let total = paragraph.match(new RegExp(search,"g")).length; or simply for(let x in paragraph) if(x==search)total++;
21st Sep 2019, 2:19 PM
Taste
Taste - avatar
+ 1
Thank you 🍇 Alex Tusinean 💜 I have tried that before but it didn't work actually. kindly check the updated question to view the code.
21st Sep 2019, 2:20 PM
Gowtham Raj
Gowtham Raj - avatar
0
Thanks Taste kindly check the updated question and let me know the answer please
21st Sep 2019, 2:32 PM
Gowtham Raj
Gowtham Raj - avatar
0
so its like this ? let wordCount = selectedText.match(new RegExp(searchText,"gm")) .length; i only added m flag to support multiline, the operation isnt safe for actual web code (ie. regex injection) but still do the trick
21st Sep 2019, 2:51 PM
Taste
Taste - avatar