Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Javascript Changing Lower case to an array after adding more item to the array?

I try to make all the trees after sorting to lowercase but For some reason, I cannot make the lower case to the listTree() work! I know that to lowercase() need to be pasted in a function in order for it to work with the array. But I am not sure how to add to the listTrees() function. Do we need to use if-else statement?

const trees = ['oak', 'Pine', 'aspen', 'Bald Cypress']

const errorElement = document.querySelector('#error')
const displayResults = document.querySelector('#displayResults')



const listTrees = () => {
    let treeList = ''
    trees.forEach(tree => {
        treeList += `${tree} <br>`
    })
    displayResults.innerHTML = `${treeList} <span>${trees.length} elements long </span>`

}

listTrees()


document.querySelector('#lowerTrees').onclick = () => {
    if (trees.length > 0) {
        trees.lowercase()
        listTrees()
    }
    else {
        errorElement.textContent = 'Dude, I cannot remove the last tree cause there are NO TREES!'
    }
    
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

toLowerCase() is only used with strings here you are using toLowerCase() with array. If you want all array items to be in lowercase you need to transform each array items like that:

document.querySelector('#lowerTrees').onclick = () => {
if (trees.length > 0) {
    trees.map((el, i) => {
       trees[i] = el.toLowerCase()
    });
    listTrees()
}
else {
    errorElement.textContent = 'Dude, I cannot remove the last tree cause there are NO TREES!'
}}
about 4 years ago · Juan Pablo Isaza Denunciar

0

For making a whole array lowerCase letters you have to iterate through the trees using array.map() and call toLowerCase() on each string:

const trees = ['oak', 'Pine', 'aspen', 'Bald Cypress']
const lowerCaseTrees = trees.map(tree => tree.toLowerCase())

Introduce it into your code by changing listTrees() slightly, you want to pass the trees as argument to it for having it dynamic:

var trees = ['oak', 'Pine', 'aspen', 'Bald Cypress']

const errorElement = document.querySelector('#error')
const displayResults = document.querySelector('#displayResults')

const listTrees = () => {
    let treeList = ''
    trees.forEach(tree => {
        treeList += `${tree} <br>`
    })
    
    displayResults.innerHTML = `${treeList} <span>${trees.length} elements long </span>`

}

// show the trees as they are
listTrees()


document.querySelector('#sortTrees').onclick = () => {
    if (trees.length > 0) {
        trees.sort()
        listTrees()
    }
    else {
        errorElement.textContent = 'No Tree'
    }
    
}

document.querySelector('#lowerTrees').onclick = () => {
    if (trees.length > 0) {
        trees = trees.map(tree => tree.toLowerCase())
        listTrees()
    }
    else {
        errorElement.textContent = 'Dude, I cannot remove the last tree cause there are NO TREES!'
    }
    
}
<article>
    <button id="sortTrees">Sort the trees A>Z</button>
    <button id="lowerTrees">Make all tress lower case</button>
</article>
<aside>
    <div id="displayResults"> </div>
    <div id="error"></div>
</aside>

Here is a handy oneliner (useful for small arrays < 1000 elements) for making whole tree to lowerCase(). This works aslong your trees will not have '##' in its name.

var trees = ['oak', 'Pine', 'aspen', 'Bald Cypress']
var lowerCaseTrees = trees.join('##').toLowerCase().split('##')
console.log(lowerCaseTrees )

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda