My current project involves developing a JavaScript program that is capable of analyzing a paragraph and extracting the sentences that include certain predetermined keywords

Looking for advice on the best approach to achieve my goal - any suggestions or tips are welcomed. I have made progress in identifying specific words, but now I need help logging the entire sentence containing that word to the console. Please advise.

**

<html>
<head>
<title> Program</title>
</head>
<body>
<script type="text/javascript">
var names = "hello hi welcome dills pills deaths kills thank you bye five";
names = names.split(" ");
var userInput = prompt("Enter Paragraph");
userInput = userInput.split(" ");
for (var i = 0; i < userInput.length;  i++) { 
for (var x = 0; x < names.length; x++) {
if (userInput[i] == names[x]) {
console.log(userInput[i])
}
}
}
</script>
</body>
</html>

**

Answer №1

Here is a concise code snippet that meets your specifications:

let phrases = "hello hi welcome dills pills deaths kills thank you bye";
phrases = phrases.split(" ");
let inputText = "Hello friends. It is cold today. I was born in Australia. I am twenty five";
let matchedPhrases = [];
phrases.forEach(word => inputText.toLowerCase().split(".").forEach(sentence => {if (sentence.includes(word)) matchedPhrases.push(sentence)}));
console.log(matchedPhrases); // [ 'hello friends', ' i am twenty five' ]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Scrolling to the next div will automatically align it in the center of the screen for the user

I am in the process of creating a single-page website and I would like to stack multiple divs on top of each other, all approximately 400px in size (with variations). Instead of using a smooth scroll, I want the page to jump to the next div and have it cen ...

What is the best way to retrieve the value from a malfunctioning HTML date input field?

I am currently developing a custom calendar picker to complement (and serve as a replacement for the default date picker provided by certain browsers) an input with type="date". It has come to my attention that Chrome's date input allows fo ...

What could be the reason for react-query searching for dispatch even when redux is not activated or present in the component?

I am currently working on a component that is supposed to fetch logged-in users from the server. Despite Swagger indicating that the server code returns correctly, the component fails to make the necessary fetch request when loaded. Below is the code snip ...

GetServerSideProps function yielding varied prop values

I'm currently exploring NextJS and delving into SSR. I've been struggling to grasp the functionality of getServerSideProps(). It seems that it should replace useState in order to be rendered on the backend, but I'm receiving different props ...

Toggle Visibility of React Component

I am new to React and I am working on creating a sidebar with a navigation menu. The goal is for the FrstComponent to open when the user clicks on the li tag with className "first", SecondComponent opens when clicking on the li tag with className "second ...

Combining arrays with integer keys into a single array in PHP

I have an array of arrays that I need to convert into a single array. Here is the array I am working with: Array ( [0] => Array ( [0] => ) [1] => Array ( [0] => 13 [1] => 9 ) [2] => ...

Enhance User Experience with JQuery Autocomplete for Internet Explorer 8 Using ASMX Web Services

Help Needed! I have been trying to implement a Web Service (ASMX) file on my website. When I access and query the page, I receive the desired results. The problem arises when I attempt to add autocomplete functionality to a textbox in my ASP.NET applicati ...

Turn off the autofill option for passwords in Google Chrome

Is there a way to prevent the password autocomplete dropdown from appearing when clicking on the password field? In Chrome Version 61.0.3163.100, none of the solutions seem to be working. Screenshot This issue has been raised many times, but setting autoc ...

Tips for updating the template URL when a button is clicked

How can I dynamically change the templateUrl from home.html to customer.html when a button is clicked? first2.directive('mycustomer', function() { return { restrict: "E", replace: 'true', templa ...

Destructuring part of an object within function parameters in JavaScript

Is it possible to selectively destructure specific object properties in function arguments, while keeping the rest accessible within the object? Let's explore this concept with a React example. (Although React is used here, the question pertains to J ...

Text at the center of a series of circles on canvas

Can someone assist me in aligning my text at the center of 5 concentric circles? Below is the code I have written, but it doesn't seem to be working. Any suggestions on what modifications I need to make to achieve this would be greatly appreciated. Th ...

The page keeps refreshing repeatedly

Can someone help me troubleshoot this url modification script? var currentPath = window.location.href currentPath=currentPath.replace(/&amp;/g, "&"); window.location.href=path; It seems like the page keeps reloading endlessly... Any sugg ...

Clicking an AngularJS button within a form is causing an unexpected page refresh

I am facing an issue with adding a new input field on click. I have two buttons, one to add and another to remove the input box. When I click on the add button, it should add a set of input boxes, but currently, it only adds one and refreshes the page, dis ...

How to extract specific data from a JSON array using PHP's array code

I have a PHP array with normal values, but one field of the array contains JSON data that I am unable to unserialize in order to extract a specific element. The array has a field named "__viewstate" which contains the data I need. I am trying to retrieve ...

Loading the central portion exclusively upon clicking a tag

Is there a way for websites to load only the middle section of the page when a link (a-tag) is clicked? I understand this is typically done through AJAX, but how does it update the URL to match the href in the a tag? I've tried using the .load functi ...

Participants will utilize functions to predict a number between 1 and 1000

I am currently working on a project that involves creating a number guessing game for the user. The idea is to have the program generate a random number between 1 and 1000, and then prompt the user to guess the number. If the guess is too low or too high ...

span tag used within a Twitter Bootstrap 2 modal

Using span structure in modal view is causing overflow issues when using .row, .row-fluid, or spans within the modal. <div id="login_register_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&g ...

Every time I rotate my div, its position changes and it never goes back to

Trying to create an eye test by rotating the letter "E" when a directional button is clicked. However, facing an issue where the "E" changes position after the initial click instead of staying in place. Here is a GIF showcasing the problem: https://i.stac ...

Encountering an Error with Polymer 1.0 and Django: Unresolved Reference to KeyframeEffect

I've been experimenting with Polymer 1.0 in combination with django and snapdragon. Although I've managed to render the elements successfully, I've hit a roadblock when it comes to animations. The code I'm using is almost identical to t ...

What is the best way to modify the colors of two specific elements using a combination of CSS and JavaScript

I am currently developing a browser-based game and have encountered an issue. Here is the relevant line of my HTML/PHP code: echo '<div id="div'.$n.'" class="d'.$rand.'" onclick="swapit(this.id, this.className)"></di ...