JSFiddle - React, Tailwind, and code Playground
JavaScript
var books = [
{
title: "Inheritance: Inheritance Cycle, Book 4",
author: "Christopher Paolini",
},
{
title: "The Sense of an Ending",
author: "Julian Barnes"},
{
title: "Snuff Discworld Novel 39",
author: "Sir Terry Pratchett",
}
]
search = prompt("Title?");
function count(pattern)
{
var results = [];
var matches = null;
pattern = new RegExp(pattern, "g")
for (i = 0; i < books.length; i++) {
results[i] = 0;
matches = books[i].title.toLowerCase().match(pattern);
if(matches != null) {
results[i] = matches.length;
}
}
return results
}
alert(count(search))