Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Unable to call Javascript function from another file

I'm trying to call the game.LeftOrRightRow function from another file, main.js, yet it isn't working. I'd like to export the object game instead of just the particular function.

I know the function itself works because when it was in the same file it did exactly what it was meant to do (return newRow). I'm also pretty sure that I've specified the right directory.

It's my first time writing Javascript so this may come down to a silly error.

The 2048.js file who's function I want to export:

const game = Object.create(null);

game.LeftOrRightRow = function (i, direction){
    let totalOne = squares[i].innerHTML;
    let totalTwo = squares[i+1].innerHTML;
    let totalThree = squares[i+2].innerHTML;
    let totalFour = squares[i+3].innerHTML;
    //the above stores the inner value of each square
    //on that row, starting from the left
    let row = [parseInt(totalOne), parseInt(totalTwo), parseInt(totalThree), parseInt(totalFour)]
    //create an array storing the entire row
    let filterRow = row.filter(num => num)
    //filter out numbers from rows and store the numbers
    //as a new array.
    let missing = 4 - filterRow.length
    //checking how many 0s there are now in the row
    let zeros = Array(missing).fill(0)
    //creating a new array filled with these 0s
    if(direction == "right"){
        let newRow = zeros.concat(filterRow)
        //If we are wanting to swipe right, the zeros
        //will be placed before the twos in our array.
        return newRow
    }else if (direction == "left"){
        let newRow = filterRow.concat(zeros)
        //If we are wanting to swipe left, the twos
        //will be placed before the zeros in our array.
        return newRow
    }
}
export default Object.freeze(game);

The main.js file where I want to call the function:

import R from "../common/ramda.js";
import Json_rpc from "./Json_rpc.js";
import game from "../common/2048.js";

document.addEventListener("DOMContentLoaded", ()  => {


...

    function moveLeftOrRight(direction){
        for(let i=0; i <16; i++){
        //loops over all of grid
            if(i%4 === 0){
            //define rows using modulus. If i MOD 4 = 0,
            //this means that the square is the start of each row.

                let newRow = game.LeftOrRightRow(i, direction)
                //newRow = game.swipeRightRow(i)
                //document.write(newRow)
                squares[i].innerHTML = newRow[0]
                squares[i+1].innerHTML = newRow[1]
                squares[i+2].innerHTML = newRow[2]
                squares[i+3].innerHTML = newRow[3]
                //replaces each value with the new array

            }
        }
    }
}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda