Uncovering hidden words within a grid of characters in JavaScript and extracting any remaining unused characters

Consider this 2D array:

[
["x","y","z"]
["m","n","o"]
["p","q","r"]
]

The provided word is :"znm" the desired result should be string : "xyzopq"

Answer №1

Here is an example of using a function for filtering characters in a multidimensional array:

let arr = [
    ["a","b","c"],
    ["d","e","f"],
    ["g","h","i"]
];

function filterCharacters(arr,word) {
    return arr // Return the character list,
      .flat(1) // converted into a 1D array,
      .filter(e => !word.includes(e)) // filtered to elements not in the word,
      .join(''); // joined together in a string.
}

console.log(filterCharacters(arr,"iea"))

Answer №2

It seems unnecessary to store the characters in a 2D array for this task. To efficiently work with them, you should consider using a 1D array instead.

const targetWord = "iea";

const charactersArray = [
  ["a", "b", "c"],
  ["d", "e", "f"],
  ["g", "h", "i"]
];

// Initialize all characters as unused
let unusedCharacters = charactersArray.flat(1);

// Loop through each row of the 2D array
for (let row = 0; row < charactersArray.length; row++) {
  // Iterate over each element in the row
  for (let col = 0; col < charactersArray[row].length; col++) {
    let currentChar = charactersArray[row][col];

    if (targetWord.includes(currentChar)) {
      // If character is used, remove from unused list
      let indexToRemove = unusedCharacters.indexOf(currentChar);
      unusedCharacters.splice(indexToRemove, 1);
    }
  }
}

console.log(unusedCharacters); // Display remaining unused characters
console.log(unusedCharacters.join("")); // Concatenate unused characters

This code snippet checks each character in the 2D array against the word and eliminates it from the list of unused characters if found in the word.

Answer №3

const letters = [
    ["x","y","z"],
    ["q","r","s"],
    ["u","v","w"]
];
console.log(letters.flat(1).filter(char=>/[^szq]/.test(char)).join(''))

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 there a way to activate and change the color of a Radio Button when it is clicked?

Is there a way to change the background-color of a clicked Radio Button so it appears highlighted? For example, when the first choice is clicked, I want it to stand out visually. This is the current UI displaying the choices: https://i.stack.imgur.com/a7 ...

Jquery Autocomplete has the ability to store and recall previous user selections

When the region is selected, autocomplete addresses should be displayed. Each time the region changes, the Jquery autocomplete function is called. While I am able to get the correct autocomplete addresses for the current region, I also receive address list ...

Issue with Ajax/Json: "An object of type triggers a circular reference error when attempting to retrieve a list."

Recently, I encountered an issue with my server-side method that is triggered through JSON/Ajax. The method itself functions flawlessly and sends back a list as expected. However, I seem to have made an error in my JavaScript code, leading to the following ...

Discover the sub strings that fall between two specified regular expressions

I am looking for a way to extract substrings from a string that are between two specified regex patterns. Here are a few examples: My $$name$$ is John, my $$surname$$ is Doe -> should return [name, surname] My &&name&& is John, my & ...

What about creating desktop applications with JavaScript?

Can Windows desktop applications be developed using JavaScript? I know about HTA (HTML Application) programs, but I'm curious if there is a more recent .NET or different solution that allows for integrating the DLL libraries from Visual Studio. ...

Expanding the dimensions of an array in C++

My challenge is optimizing a function that saves data on a 2D array and sends it over the internet 50 times per second. To minimize size wastage, I aim to adjust the second dimension of the array based on the number of updates in that fraction of a second. ...

Why does a boolean value behave this way in JSX, specifically when used in a checkbox scenario?

I am completely new to React/JSX and I'm struggling to grasp the logic behind how things function. For instance, <input type="checkbox" checked=true /> results in <input type="checkbox checked />. On the other hand, <inp ...

Angular/RXJS - Synchronize with AJAX response before proceeding

export class DropDownService { private dockerURL; constructor(private infoService: InfoService){ this.infoService.getVersion().subscribe((URL) => { this.dockerURL = URL; }); // Ensuring the ...

comparing values in an array with jquery

I am attempting to retrieve the phone number and mobile number from an array using jquery. jQuery: var data = $('#PhoneLabel').text(); var array = data.split(', '); $.grep(array, function (item, index) { if (item.charAt(0) === &ap ...

JavaScript Filtering JSON Data Based on Date Range

I am attempting to filter a basic JSON file based on a specified date range, with both a start date and an end date. Below is the function I have created for this task: var startDate = new Date("2013-3-25"); var endDate = new Date("2017-3-2 ...

What is the best way to retrieve the values from the labels for two separate buttons?

I designed a pair of buttons with a single label below. Both buttons acted as standalone entities. <label for="buttons" class ="val">0</label> <input class="btn btn-primary button1" type="button" ...

What is the best way to assign an image to a div using JavaScript or jQuery?

I'm attempting to create a notification box similar to this one: https://i.sstatic.net/HIJPJ.png As shown, there is a .gif image positioned at the top that will be hidden once the content loads. Now, I want to insert the following image into a <d ...

Purge the cache of CSS and JavaScript in your browser

Is there a way to clear JavaScript and CSS cache in ASP MVC without having to clear browser history? ...

Can a CSS hover effect transition modify the color scheme?

I have multiple buttons on a single page, and I want the hover effect to be applied to all of them with just one code using jQuery. Please review my code below. $(document).ready(function(){ $('.button').hover(function(){ var bc=$(this ...

Substituting elements within a string formula by utilizing a matrix and looping through columns

Column X contains variables with values in each column j. In this case, only U1, X4, and U2 have values, while the other variables from the list ['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', ...

Post-render for HTML linkage

Is there a method to execute customized code after Knockout has inserted the html into the DOM and completed rendering? This is required in order to bind a nested view model to dynamically generated html code. Perhaps like this: <div data-bind="html: ...

Can the environment variables from the .env package be utilized in npm scripts?

I've integrated dotenv into my cypress project and defined variables in a .env file, as shown here: USER=Admin How can I utilize the env variable USER within my npm scripts? "scripts": { "cypress:open": "npx cypress ope ...

Differences between variable scope in Node.js and web browsers when running JavaScript code

When it comes to browser, different JavaScript files share one scope: a.js: var a=1; //by adding "var", we prevent it from becoming a global variable. b.js: console.log(a) //even though a=1, b.js can still access this variable! In Node.js: a.js .... b ...

Locating the element containing the highest even digit among various arrays

I'm struggling to identify the element with the highest even digit in a series of arrays. My current code only deals with positive numbers, completely dismissing any negative values. int main() { int ary1[] = { 123, 456, -7890, 12 }; int ary2[] = ...

What could be causing the issue with res.clearCookie() not functioning properly post deployment on Vercel?

Initially, the application functions flawlessly on localhost. However, upon deployment to Vercel, an issue arises when users attempt to log out and the cookies are not clearing as intended with res.clearCookie(). Consequently, even after a page refresh, t ...