0

I'm trying to create a game similar to a crossword. I am storing my letters into this array. Can I please have some help extracting the words from this 2D array of mine?

let testArr = [
    ["T", " ", " ", " "],
    ["E", "A", "R", " "],
    ["S", " ", " ", " "],
    ["T", " ", " ", " "]
];

let storeWords = [];

function loop() {
    let storeRowLetters = [];
    let storeColLetters = [];

    for (let col = 0; col < testArr.length; col++) {
        for (let row = 0; row < testArr[col].length; row++) {
            console.log(testArr[row][col])
            storeColLetters.push(testArr[row][col]);
        }
    }

    console.log("Next output scans the row")
    for (let col = 0; col < testArr.length; col++) {
        for (let row = 0; row < testArr[col].length; row++) {
            console.log(testArr[col][row])
            storeRowLetters.push(testArr[col][row]);
        }
    }

    console.log(storeColLetters.join(''))
    console.log(storeRowLetters.join(''))
}

loop()

The outputs I receive from the console log are enter image description here

From the last 2 lines of my picture, I would like to extract the word TEST from the string of TEST A R and EAR from T EAR S T. So basically I would like to discard the letters that are on their own. After acquiring the words, I would like to push them to my array of storeWords.

Jeboris
  • 49
  • 5

2 Answers2

0

Here how you can iterate through a 2D array

var cubes = [
 [1, 2, 3],
 [4, 5, 6],    
 [7, 8, 9],
];

for(var i = 0; i < cubes.length; i++) {
    var cube = cubes[i];
    for(var j = 0; j < cube.length; j++) {
        display("cube[" + i + "][" + j + "] = " + cube[j]);
    }
}

you can check For loop in multidimensional javascript array

and you can filter by

var arr = [{"nid":"31","0":{"tid":"20","name":"Bench Press","objectDate":"2012-02-08","goal":"rep","result":"55.00","comments":"sick!","maxload":"250"},"1":{"tid":"22","name":"Back Squat","objectDate":"2012-02-08","goal":"time","result":"8.00","comments":"i was tired.","maxload":"310"}},{"nid":"30","0":{"tid":"19","name":"Fran","objectDate":"2012-02-07","goal":"time","result":"5.00","comments":null}}];


function filterByProperty(array, prop, value){
    var filtered = [];
    for(var i = 0; i < array.length; i++){

        var obj = array[i];

        for(var key in obj){
            if(typeof(obj[key] == "object")){
                var item = obj[key];
                if(item[prop] == value){
                    filtered.push(item);
                }
            }
        }

    }    

    return filtered;

}

var byName = filterByProperty(arr, "name", "Fran");
var byGoal = filterByProperty(arr, "goal", "time");

find more details here : How to filter multidimensional JavaScript array

0

You could collect temporary words and store, if you see a space or quit the loop.

const
    array = [["T", " ", " ", " "], ["E", "A", "R", " "], ["S", " ", " ", " "], ["T", " ", " ", " "]],
    words = [],
    lengths = [array.length, array[0].length];

for (let j = 0; j < lengths[1]; j++) {
    let temp = '';
    for (let i = 0; i < lengths[0]; i++) {
        if (array[i][j] !== ' ') {
            temp += array[i][j];
            continue;
        }
        if (temp.length > 1) words.push(temp);
        temp = '';
    }
    if (temp.length > 1) words.push(temp);
}        

for (let i = 0; i < lengths[0]; i++) {
    let temp = '';
    for (let j = 0; j < lengths[1]; j++) {
        if (array[i][j] !== ' ') {
            temp += array[i][j];
            continue;
        }
        if (temp.length > 1) words.push(temp);
        temp = '';
    }
    if (temp.length > 1) words.push(temp);
}

console.log(words);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392