JS- Date,Strings,Arrays & Functions i-assess 1) Question
Sat Oct 05 2024 05:51:40 GMT+0000 (Coordinated Universal Time)
Saved by
@signup
#html
#javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Word Search</title>
<script>
function searchWord() {
const paragraph = document.getElementById('paragraph').value;
const searchWord = document.getElementById('search').value.trim();
const wordsArray = paragraph.split(/\s+/);
let count = 0;
for (let word of wordsArray) {
if (word.localeCompare(searchWord, undefined, { sensitivity: 'base' }) === 0) {
count++;
}
}
const resultDiv = document.getElementById('result');
// Adjusting the message to match the expected output
if (count > 0) {
resultDiv.innerHTML = `Searched text ${searchWord} is present ${count} times in the paragraph.`;
} else {
resultDiv.innerHTML = 'Searched text not found'; // Ensured no extra formatting
}
}
</script>
</head>
<body>
<h2>Search Word</h2>
<textarea id="paragraph" rows="10" cols="50" placeholder="Enter your paragraph here..."></textarea><br><br>
<input type="text" id="search" placeholder="Enter word to search...">
<button id="searchWord" onclick="searchWord()">Search</button>
<div id="result" style="margin-top: 20px; font-weight: bold;"></div>
</body>
</html>
content_copyCOPY
Comments