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

275
Visualizações
Array in JavaScript is undefined

I am very new to JavaScript but have good experience with C and java. For whatever reason my code below will not fill in the array with values other than undefined. I'm trying to simply fill an array with a random set of numbers for a blackjack game

let randomSuite;
let randomNum
let count = 0;
var cards = new Array(56);
window.onload = function main(){
const suites = [
    "H",
    "D",
    "S",
    "C"
];

for(let i = 0 ; i < 56 ; i++){
    randomNum = (Math.random * 12) + 1;
    randomSuite = Math.random * 3;
    cards.push = randomNum;
    console.log(cards[i]);
    count++;
}
alert(cards[1]);
}
function hitFunc(){
    alert("works " + cards[0]);
}
*{
    margin: 0;
    padding : 0;
    font-family: sans-serif;
}

.main{
    width: 100%;
    height: 100vh;
    background-color:black;
}

.img1{
    position: relative;
    z-index: -1;
    margin: 10px auto 20px;
    display: block;
    width: 75%;
    height: 75%;
}

.img2{
    position: relative;
    z-index: 1;
    margin: 10px auto 20px;
    display: block;
    bottom: 200px;
    right: 400px;
    width: 7%;
    height: 7%;
}

.hitButton {
z-index: 1;
position: relative;
text-align: center;
left: 225px;
bottom: 550px;
}

.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
    color: aliceblue;
    object-position: center;
  }

Here's what I have. The alerts are used to show that the function is being completed. Any help is appreciated Please leave an explanation as well. This is my first post on stack overflow let me know if there's anyway to improve the quality of my post.

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

0

  1. Math.random is a function; use Math.random()
  2. Same for push, use cards.push(randomNum)
  3. You were defining an array with 56 spots new array(56) but since you're using push you'll need to create an empty array so you're pussing to the desired index. Otherwise, instead of push, just set it on the index: cards[i] = randomNum
  4. No need for the count variable since the loop iterator (i) has the same value

let randomSuite;
let randomNum
var cards = new Array();

window.onload = function main(){
    const suites = [
        "H",
        "D",
        "S",
        "C"
    ];

    for(let i = 0 ; i < 56 ; i++){
        randomNum = (Math.random() * 12) + 1;
        randomSuite = Math.random() * 3;
        cards.push(randomNum);
    }
    console.log(cards)
}
*{
    margin: 0;
    padding : 0;
    font-family: sans-serif;
}

.main{
    width: 100%;
    height: 100vh;
    background-color:black;
}

.img1{
    position: relative;
    z-index: -1;
    margin: 10px auto 20px;
    display: block;
    width: 75%;
    height: 75%;
}

.img2{
    position: relative;
    z-index: 1;
    margin: 10px auto 20px;
    display: block;
    bottom: 200px;
    right: 400px;
    width: 7%;
    height: 7%;
}

.hitButton {
z-index: 1;
position: relative;
text-align: center;
left: 225px;
bottom: 550px;
}

.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
    color: aliceblue;
    object-position: center;
  }

about 4 years ago · Juan Pablo Isaza Relatório

0

Call functions with parenthesis

Math.random() and push() are functions not variables therefore you need to call them using ().

push() will append to the array

push() will append a value to the array. You already initialize an array with 10 values. Pushing new values will increase the array size but not add values in the first 10 spots.

const array = new Array(10);

for(let i = 0; i < 10; i++){
  array.push(i);
}
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Solution

You should instead use array[i] = value; to set a value on a given position.

const array = new Array(10);

for(let i = 0; i < 10; i++){
  array[i] = i;
}
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you want to have a more functional way of doing this as is typical for JavaScript you could use map():

const array = [...new Array(10)].map((_, index) => index);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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