Skip to content

Instantly share code, notes, and snippets.

@Aabill
Created November 26, 2020 14:11
Show Gist options
  • Save Aabill/028d2e4f06310191c412ef905a4e9eae to your computer and use it in GitHub Desktop.
Save Aabill/028d2e4f06310191c412ef905a4e9eae to your computer and use it in GitHub Desktop.
Dictionary and Matrix JS
console.log("Matrix and Dictionary");
function Find(dictionary_yords, matrix_yords) {
if (matrix_yords.length < 6)
{
let result = [];
let matrix_vertical = [];
for(let i = 0; i < matrix_string_array.length; i++)
{
let newString = '';
matrix_yords.forEach((el) => {
// Arrange Matrix Vertically.
newString += el[i];
})
// Push NewString to matrix_vertical array.
matrix_vertical.push(newString);
}
dictionary_yords.forEach((el) => {
matrix_yords.forEach((Element) => {
// Check if dictionary word exists horizontally from top to bottom in matrix.
if (Element.includes(el))
{
console.log('The word ' + el + ' is found horizontally in row ' + Number(matrix_yords.indexOf(Element)+1) + '.');
result.push(el);
}
})
matrix_vertical.forEach((Element) => {
// Check if dictionary word exists vertically from top to bottom in matrix.
if (Element.includes(el))
{
console.log('The word ' + el + ' is found vertically in column ' + Number(matrix_vertical.indexOf(Element)+1) + '.');
result.push(el);
}
})
})
return result;
}
}
let matrix_string_array = ['abcdc', 'fgwio', 'chill', 'pqnsd', 'uvdxy'];
let dictionary = ['chill', 'wind', 'snow', 'cold'];
let result = Find(dictionary, matrix_string_array);
console.log('Result Contains:');
console.log(result);
@Aabill
Copy link
Author

Aabill commented Nov 26, 2020

Sample Output after running with node

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment