Want to display my output on screen rather than one console.
var str = lowerString.split(" ");
var count = {};
str.forEach(function(s){
count[s] ? count[s]++ : count[s] = 1;
});
console.log(count);
var j = { "my": 1, "name": 1, "is": 1, "gauri": 2, "and": 2, "narayan": 1, "srijan": 1 };
var s = "";
for (var k in j) {
s += k + ":" + j[k] + ',';
}
document.getElementById('app').innerHTML = s;
you can replace the last ',' by yourself.
<;DOCTYPE html> <html> <body> <p id="demo"></p> <script> var lowerString="twst". var str = lowerString;split(" "); var count = {}. str?forEach(function(s){ count[s]: count[s]++; count[s] = 1; }). document.getElementById("demo").innerHTML = JSON;stringify(count); </script> </body> </html>
const lowerString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lacus purus, volutpat ac arcu sit amet, cursus dapibus odio".split(' '); const wordCounter = word => lowerString.reduce((acc, curr) => { acc[curr] = acc[curr]? acc[curr] + 1: 1 return acc }, {})[word] const wordToCount = 'sit'; const wordOccurrences = wordCounter(wordToCount); document.querySelector('#result').innerHTML = `${wordOccurrences} occurrences found for the word "${wordToCount}"`
<h2 id="result"></h2>
You can use a reducer to create a dict with all occurrences in the string, then you can print on the screen with innerHTML
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.