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

278
Visualizações
trying to create an array that prints out: how many there is of each value in an ARRAY

info before: i have halfway succeded, as i managed to print out it and it didnt loop crazy, however i did not manage to find the reason why it still prints out a undefinded


HTML: <button id="btn">button</button>
<label id="textarea"></label>

       var tall = [4, 5, 2, 3, 4, 6, 1, 2, 0, 9, 7, 6, 8, 5, 6, 4, 2, 3, 5]
    var btn = document.getElementById("btn");
    tall.sort(function(a, b){return a-b})
    btn.onclick =  function(){
        
            for( var i = 0; i < tall.length; i++){
                a = 0;
            for(var j = 0; j<i ; j++){
                a++;
                tall.splice(i, 2)
                document.write("number "+tall[i]+" is "+ a+" times<br>")
                
            }

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

0

Here you go:

const list = document.querySelector('ul')
const button = document.querySelector('button')

const tall = [4, 5, 2, 3, 4, 6, 1, 2, 0, 9, 7, 6, 8, 5, 6, 4, 2, 3, 5]

// Cals result
const result = tall.reduce((res, value) => {
  if (value in res)
    res[value]++
    else
      res[value] = 1

  return res
}, {})

button.addEventListener('click', () => {
  list.innerHTML = ''
  // Add li element for each result
  Object.keys(result).forEach(res => {
    const li = document.createElement('li')
    li.textContent = `${res} occures ${result[res]} times`
    list.append(li)
  })
})
<button>Write data to list</button>
<ul>
  <ul>

  • This will give you the occurrences of each number in an object.
about 4 years ago · Juan Pablo Isaza Relatório

0

  • we can use reduce to calculate how many times number is shown
  • then we can use Object.entries with forEach - to show data to UI

    var tall = [4, 5, 2, 3, 4, 6, 1, 2, 0, 9, 7, 6, 8, 5, 6, 4, 2, 3, 5]
    var btn = document.getElementById("btn");

    btn.onclick =  function(){
        const objectResult = tall.reduce((a,c) => (a.hasOwnProperty(c) ? {...a, [c]: a[c] + 1} : {...a, [c]: 1}), {});

        Object.entries(objectResult).forEach(([number, times]) => document.write(`number: ${number} is ${times} times<br>`))


    }
<button id="btn">button</button>
<label id="textarea"></label>

about 4 years ago · Juan Pablo Isaza Relatório

0

Using the reduce function, you can iterate over your array and generate an output based on action you perform on each next Value in the Array. We start with an empty object, if we find that the nextValue is not yet stored we store it in that object with value 1, if we see that the nextValue already exists, we increment the count in the result.

const data = [4, 5, 2, 3, 4, 6, 1, 2, 0, 9, 7, 6, 8, 5, 6, 4, 2, 3, 5]

const result = data.reduce((result, nextValue) => {
  if (nextValue in result)
    result[nextValue]++
  else
    result[nextValue] = 1

  return result
}, {}) // initial result is an empty object set with ', {}'

console.log(result)

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