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

142
Visualizações
How can I half the array

I am learning to code and I am trying out this javascript object method course. I am currently stuck on this method. I want to have the array with three different numbers(10,20,5) to be /2. I do not understand why it is returning NaN. Thank you for reading.

shirtPrice = 10
jeanPrice = 20
shoePrice = 5

shoppingList = [shirtPrice,jeanPrice,shoePrice];

const shoppingDiscountDay = {
  discountedItems: {
    calculateItemsDiscount() {
        return shoppingList/2; //return NaN
    },
  }
}
console.log(shoppingDiscountDay.discountedItems.calculateItemsDiscount());
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try using .map()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

const shoppingDiscountDay = {
  discountedItems: {
    calculateItemsDiscount() {
        const itemsHalfPrice = shoppingList.map(item=>item/2)
        return itemsHalfPrice;

    },
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot divide an array, you need to divide each of the elements in it.

You can use the .map method which applies a function on each of the elements of an array and returns a new array with the results

shirtPrice = 10
jeanPrice = 20
shoePrice = 5

shoppingList = [shirtPrice, jeanPrice, shoePrice];

const shoppingDiscountDay = {
  discountedItems: {
    calculateItemsDiscount() {
      return shoppingList.map(itemPrice => itemPrice / 2);
    },
  }
}
console.log(shoppingDiscountDay.discountedItems.calculateItemsDiscount());

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't perform '/' operation on array. It is meant to be perform on numbers. Instead try this.

shirtPrice = 10
jeanPrice = 20
shoePrice = 5

shoppingList = [shirtPrice,jeanPrice,shoePrice];

const shoppingDiscountDay = {
  discountedItems: {
    calculateItemsDiscount() {
       for(let i = 0;  i < shoppingList.length; i++){
          shoppingList[i] = shoppingList[i]/2;  
       }
        return shoppingList; 
    },
  }
}
console.log(shoppingDiscountDay.discountedItems.calculateItemsDiscount());
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