Look for specific terms within an array of words

Hello, I am facing a challenge with searching for words that contain at least part of another word in JavaScript. Specifically, I need to find words in an array without using built-in functions like index.of, slice, substr, substring, or regex. Can only use the PUSH function.

Here is my current code snippet:

function wordsChecker(sentences, search) {
    var result = []
    for (var i = 0; i < sentences.length; i++) {
        var isCheck = false
        for (var j = 0; j < sentences.length; j++) {
            for (var k = 0; k < search.length; k++) {
                if (sentences[i][j] == search[k]) {
                    isCheck = true
                }
            }
        }
        if (isCheck == true) {
            result.push(sentences[i])
        }
    }
    return result
}

console.log(wordsChecker(['broom, room, rox, show room, stroom, root, rote, brother'], 'roo'))

//expected output : [broom, room, show room, stroom, root]

Thank you in advance for your help!

Answer №1

To start off, it is essential to create an accurate array structure. The following code snippet is designed to assist you in achieving this goal.

function searchWords(phrases, target) {
    var results = [], key = 0;
    for (var idx = 0; idx < phrases.length; idx++) {
        var isMatched = false;
        for (var jdx = 0; jdx < phrases[idx].length; jdx++) {
            while (key < target.length) {
                if (phrases[idx][jdx] == target[key]) {
                    isMatched = true;
                    key++;
                    break;
                } 
                else {
                  isMatched = false;
                  key = 0;
                  break;
                }
            }
        }
        if (isMatched === true && key === target.length) {
            results.push(phrases[idx]);
            key = 0;
        }
    }
    return results;
}

console.log(searchWords(['broom', 'room', 'rox', 'show room', 'stroom', 'root', 'rote', 'brother'], 'roo'));

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

Is it possible to send a Word file as an email attachment using PHP's mail() function

When I sent a Word document file via email, it generated an unknown format like asdfADFDF 0000sadfas15454454. The mail function was used to send the email. Below is the code for that. Please review and let me know: function sendfile() { $id = $_REQU ...

Maintain the div in its current location when zooming the map

I created a multi-layer map and added a div to it. However, the issue arises when I zoom in on the image, causing the div to change its position unexpectedly. For example, if I place the div over India and then zoom and drag the image, the div ends up in U ...

The modal window pops up immediately upon the first click

Experience a dynamic modal element that springs to life with just the click of a button or an image. The magic lies in the combination of HTML, CSS, and jQuery code: <div id="modal-1" class="modal"> <div class="button modal-button" data-butto ...

When using Google Maps Autocomplete, always make sure to input the full state name instead of just the state

Utilizing the Google Maps autocomplete API, we have enabled our customers to search for locations in the format of city, state, country. The functionality is effective overall, but a recurring issue arises when searching for cities, such as 'Toronto&a ...

A demonstration of VueJS Nested Builder functionality

I am currently exploring VueJS and working on a project to create a simple page builder application that allows users to add sections and subsections. I am seeking guidance on how to properly set up the Vue data property for this functionality. Any advice ...

How to achieve multiplication in Javascript without utilizing the * operand

Challenge 1 Criteria: This problem involves working with two positive integers N and M. Outcome: Upon completion, the function should output the result of multiplying N and M. For instance, if you input 5 and 8 into the function, it should calculate and ...

Upon reloading the page, the Vue getter may sometimes retrieve an undefined value

My blog contains various posts. Clicking on a preview will direct you to the post page. Within the post page, I utilize a getter function to display the correct post (using the find method to return object.name which matches the object in the array). cons ...

How can data trigger dynamic JavaScript loading and event subscription?

I am currently working on a JavaScript client that utilizes PubNub for messaging. I subscribe to a channel in the following manner: pubnub.subscribe({ channel : chnl, message : function(m){ var msg = $.parseJSON(m); ...

Creating a link with a POST method and without using a new line

I am attempting to generate a square matrix and send data using the post method. I have discovered a solution involving JavaScript and form submission, but each time a new form is created, it results in a new line being added. Alternatively, if I move th ...

Automatically sync textbox width with gridview dimensions

My goal is to dynamically resize a number of textboxes so that they match the width of my gridview's table headers. The gridview will always have the same number of columns, but their widths may vary. However, as shown in the image below, the width va ...

"Successful implementation of Ajax function in local environment but encountering issues when running online

I am facing an issue with my AJAX function. It works perfectly fine on my local server but does not return anything when I move it to my online server. Below is the code snippet: This is the part of the page where I call the showEspece() function: echo ...

To avoid any sudden movements on the page when adding elements to the DOM using jQuery, is there a way to prevent the page from

I have a challenge where I am moving a DIV from a hidden iFrame to the top of a page using jQuery. Here is my current code: $(document).ready(function($) { $('#precontainer').clone().insertBefore(parent.document.querySelectorAll(".maincontainer" ...

Creating an element that remains a consistent height despite zooming or resizing the window: A guide with HTML and CSS

Recently, I created a custom scrollbar but faced an issue with its width. Whenever I zoom in or out (Ctrl + mousewheel), the width (set to 10px) changes due to being in pixels. When switched to width: 1vw, it works well with zooming but gets affected by wi ...

Determine the total number of string elements within a string array using the C programming language

char* names[]={"X", "Y", "Z"}; How can the total number of strings in the array be determined? In this instance, the output should be 3. ...

"Efficiently rendering a variety of textures using a single draw

Hey there, I am interested in creating multiple cubes with unique textures for each cube. In order to optimize performance, I've combined the geometries into a single mesh. However, I've encountered an issue with textures as I currently require a ...

Why is it beneficial to include multiple script blocks on a webpage?

Note: After reviewing the feedback from Andrew Moore, it seems that this question is a duplicate of Two separate script tags for Google Analytics?. Therefore, there may be merit in removing this question to prevent clutter on Stack Overflow. However, if th ...

Bootstrap: Retrieve an image from a modal

I am working on a modal that contains a variety of selectable images. When an image is clicked, I want to change the text of the button in the modal to display the name of the selected image. Additionally, I would like to grab the selected image and displa ...

After implementing the ng-repeat directive, the div element vanishes

I've been working on fetching data from a Json API and displaying it on user event. However, I'm facing an issue where the div disappears whenever I apply the ng-repeat property to it. Despite searching through various tutorials and documentation ...

Learn the art of blurring elements upon clicking in Vue

I've been attempting to trigger the blur event on an element when it is clicked, but I haven't been able to locate any helpful examples online. My initial approach looked like this: <a @click="this.blur">Click Me</a> Unfortunately, ...

Leveraging react props with the .map method

Imagine having this render function in one of your components. You've passed a changeTid prop function from the parent element. Parent Component: <RequestsList data={this.state.data} changeTid={this.changeTid} /> Child Component: (Using ES6 ...