examine and contrast two arrays

function checkArray(arr) {
  //target array to check against
  const targetArray = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ]

  for (let i = 0; i < targetArray.length; i++) {
    compareArrays(arr, targetArray[i])
  }
}

function compareArrays(supplied, subArray) {
  supplied.sort()
  subArray.sort()
  let i = 0
  let j = 0
  for (i, j; i < supplied.length && j < subArray.length;) {
    if (supplied[i] < subArray[j]) {
      ++i
    } else if (supplied[i] == subArray[j]) {
      ++i, ++j
    } else {
      return false
    }
  }
  return j == subArray.length;
}


checkArray([1, 3, 7, 4])

I'm attempting to create a function that checks if every element in an array is present in any of the nested arrays within the object.

I referred to a function from this post, but I am puzzled as to why I am receiving undefined output instead of true.

Answer №1

If you are looking to determine if any combination of numbers from the winningIds array is present in the arr array, here is a sample code implementation:

function findWinningCombo(arr) {
  // Array containing winning combinations
  const winningIds = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ]

  var hasWinner = false;

  for (let i = 0; i < winningIds.length && !hasWinner; i++) {
     hasWinner = checkCombination(arr, winningIds[i])
  }

  return hasWinner;
}

function checkCombination(mainArray, subArray) {
  mainArray.sort()
  subArray.sort()

  for (var i = 0; i < subArray.length; i++) {
    if (mainArray.indexOf(subArray[i]) == -1) {
        return false;
    }
  }
  return true;
}

findWinningCombo([1, 3, 7, 4]);

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

Save this text in HTML format to the clipboard without including any styling

When using this code to copy a htmlLink to the clipboard: htmlLink = "<a href='#'>link</a>"; var copyDiv = document.createElement('div'); copyDiv.contentEditable = true; document.body.appendChild(copyDiv); ...

What is the best way to extract the modal into its own file?

Greetings, fellow React developer who is new to the field! I have successfully implemented a React Class Component that includes a popup Modal. This Modal displays when a user clicks on an Item Card: import React from 'react'; import { Card, Bu ...

Move all elements in a C++ array by one spot in order to insert a new element at a specific position

Struggling with a small function for a homework assignment, seeking assistance. I have a static array with a size of 20 (shelfSize), but I only need to utilize a maximum of 10 elements. Hence, no need to worry about out of bounds errors (initializing the ...

Having difficulty adding a border to the SlidesJS frame

I am utilizing the SlidesJS jQuery plugin which can be found here. My goal is to add a border around the slider. I adjusted the CSS section like so: #slides .slidesjs-container { margin-bottom:10px; border-color: #ff5566; border-width: 3px; borde ...

Accessing a child field from Firebase in a React Native application

My firebase data is structured like this: "Locations" : { "location01" : { "image" : "https://www.senecacollege.ca/content/dam/projects/seneca/homepage-assets/homepage_intl.jpg", "instructorNa ...

Ways to clear all information from a struct array that was not allocated dynamically

I am working with the C language and I am looking to create a function that can clear all the data from an array of structs when called. The array is not dynamically allocated, so using the free function will not work. Is there another method other than ...

It is my goal to populate the array with numbers while avoiding any duplicate entries

I am currently facing an issue with my code as it is returning a length of 0 instead of the expected result of 5 after excluding duplicates from the array. I have a feeling that there might be something missing in my code, but I just can't seem to fig ...

How to toggle the visibility of a search bar in ReactJS?

I am trying to implement a simple search bar in reactjs without a dedicated close button. I want the same button to toggle between opening and closing the search bar. Below is the code snippet I have experimented with, but unfortunately, it's not fun ...

Router.push and Link are failing to update the page despite the URL being refreshed in Next.js

I regret that my explanation of the issue may not have been clear. Below is a link that accurately describes the problem I am facing. Any assistance on this matter would be greatly appreciated. The directory path I am working with is pages/request/[reqid] ...

The code snippet for adding a class using jQuery with $(this) seems to be malfunctioning, and

I'm facing an issue with this code, it's supposed to add a class when clicked on but it's not working. $('a').click(function(){ //alert('on'); WORKING $(this).addClass('on'); }) The HTM ...

Which is the Better React Entry Point: Index.html or index.js? And where exactly can we find the Node.js

After doing some research online, I came across conflicting information regarding the entry point for a react app. One source claimed that index.html serves as the entry point, while another stated that index.js is actually the main entry point for both Re ...

Retrieve all numeric or undefined keys from an array using PHP

I need help with extracting elements from a multidimensional array that do not have named keys. For example: Array { ['settings'] {...} ['something'] {...} [0] {...} // Looking for this element ['something_else'] {.. ...

Creating stunning visuals with the power of SVG

I need to create a graphics editor similar to Paint using SVG for a school project. I have JavaScript code for drawing shapes like circles and lines, but when I try to add them to an onClick function for a button, it doesn't seem to be working. funct ...

Every character entered in JSP should trigger an instant retrieval of the corresponding string in the servlet

Having a JSP file that contains a text field: <form action="someServlet" method=post> <input type ="text" name="user" id="uname"> <button type="submit" id="submit">Submit</button> </form> When typing each letter in the JSP, ...

Unable to load website after inserting PHP script into the code

Below is the javascript code that I have written: <script type="text/javascript"> $(document).ready(function(){ $("#sepatu").click(function(e){ e.preventDefault(); var site_url = "<?php echo base_url()?>i ...

The Material UI component successfully loads the options data list from the server response, however, it fails to return the selected value when an option

My goal is to dynamically populate my country and province select component based on server response data. The process begins with the user selecting a country, which triggers a request to the server using the selected country's id. The server respond ...

What is the best way to address background-image overflow on a webpage?

I'm facing a challenge in removing the overflow from a background-image within a div. There are 4 divs with similar images that combine to form one background image (I adjust the position of each image so they align across the 4 divs). Essentially, I ...

Using Angular5 to extract coordinates from the Google Maps v3 API when a map "click" event occurs

Can anyone assist me with retrieving coordinates from a click event on a map? Here is the desired result: Link Below is the code I have so far: import { Component, OnInit } from '@angular/core'; import { ViewChild } from '@angular/core&ap ...

Activate a Bootstrap tab using turbolinks dynamically

After loading the page, I am attempting to open a Bootstrap 4 tab. It works when I refresh the page, but if I navigate within the site, it gives me a query selector empty error. This code is a port of the solution mentioned in this tutorial, as I am using ...

Choose all options within the optgroup group

I am facing an issue with a select element that contains grouped options. My requirement is to be able to select or deselect all options within an optgroup when a specific option is clicked. Furthermore, I need the functionality to allow multiple optgroups ...