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

181
Visualizações
elements in my array is undefined, but outside cycle they are good

Why my elements of arrays P0 and P1 is undefined? They are undefined in cycle "while (run === true)". Outside this cycle they equals 0

function Q(x, y) {
    return 10000;
}

function Create2DArray(rows) {
    var mas = [];
    for (var i = 0; i < rows; i++) {
        mas[i] = [];
        for (var j = 0; j < rows + 1; j++) {
            mas[i][j] = 0;
        }
    }
    return mas
}

let t = 0, tmax = 50, RightX = 250, h = 10, d = 19, K = 0.4, wind = 0;

let N = RightX / h
console.log(N);
C = 5

let alpha = 10 * Math.PI / 180

let U = C * Math.cos(alpha)
let V = C * Math.sin(alpha)
let M1, M2, M3, M4
let D = d * C;

let P0 = Create2DArray(N)
let P1 = Create2DArray(N)
console.log(P1);

let MinVal = h / U

if (MinVal > h / V) {
    MinVal = h / V
}

if (MinVal > ((h * h) / (2 * D))) {
    MinVal = (h * h) / (2 * D)
}

let tau = K * MinVal

let i = 0, j = 0;

t = t + tau
let run = true
while (run === true) {
    for (i = 0; i <= N - 1; i++) {
        for (j = 0; j <= N - 1; j++) {
            if (U > 0) {
                M1 = P0[i][j] * U * h * tau
                M2 = P0[i][j - 1] * U * h * tau
            }

            else if (U < 0) {
                M1 = P0[i + 1][j] * U * h * tau
                M2 = P0[i][j] * U * h * tau
            }

            else if (V > 0) {
                M3 = P0[i][j] * V * h * tau
                M4 = P0[i][j - 1] * V * h * tau
            }

            else if (V < 0) {
                M3 = P0[i][j + 1] * V * h * tau;
                M4 = P0[i][j] * V * h * tau;
            }
            else {
                console.log("test");
            }
            S1 = P0[i][j]
            S2 = (tau / (h * h)) * D * (P0[i + 1][j] - 4 * P0[i][j] + P0[i][j] + P0[i][j + 1] + P0[i][j])
            S3 = 1 / (h * h) * (M1 - M2 + M3 - M4)
            S4 = Q(i, j) * tau
            P1[i, j] = S1 - S3 + S2 + S4
        }

        for (let z = 0; z <= N + 1; z++) {
            P1[0, z] = 0;
            P1[N + 1, z] = 0;
        }

        for (let z = 0; z <= N + 1; z++) {
            P1[z, 0] = 0;
            P1[z, N + 1] = 0;
        }

        for (let z = 0; z <= N + 1; z++) {
            for (let r = 0; r <= N + 1; r++) {
                P0[z, r] = P1[z, r]
            }
        }
    }
    t = tau + t
    if (t > tmax) {
        run = false
    }
}

I tried to change the methods for creating two-dimensional arrays, I also tried to move the loop and everything worked, however, in such a construction, the elements of the array P0 and P1 are equal to undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In javascript the "," operator return the right value. So when you write PO[z, r], it's like you write PO[r]. I guess you want to write PO[z][r] = P1[z][r] instead.

about 4 years ago · Juan Pablo Isaza Relatório
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