Slip 15 - A) Create a Node.js Application to count occurrence of given word in a file and display the count on console.

Solution:

 var fs = require('fs')


function countOccurences(string, word) {

    return string.split(word).length - 1;

}

var text = fs.readFileSync('demo.txt', 'utf8');

var count = countOccurences(text, "and"); 

if(count==0){

    console.log("not found")

}else{

console.log(count);

}

Post a Comment

0 Comments