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

124
Visualizações
Display the average price of a book

I need some code that implements calculating the average price of books, type of ROMAN. The type field can be written in any case (roman, ROMAN).

let books = [
  { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
  { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
  { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
  { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
  { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
  { name: 'Some-6', author: 'Hohol', type: 'tale', price: 366.0 }
];

So I derived all types with this method:

const getType = books.map(({type}) => type.toLowerCase())

And further my problem is that I don't know how got access to price

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

0

You can do that in several Method

You can loop trough the array and sum up all the prices there and then devide on its length, also you will check if the current object type equals roman, by using .toLowerCase()

if you want to return the natural number you can use .toFixed() method

For Loop

let books = [
    { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
    { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
    { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
    { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
    { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
    { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
 ]
 
let TotalPrice = 0;
let romanPriceLength = 0;

for (let i = 0; i < books.length; i++) {
  if ((books[i].type).toLowerCase() === "roman") {
    TotalPrice += books[i].price
    romanPriceLength++
  }
}

console.log(TotalPrice / romanPriceLength)
console.log((TotalPrice / romanPriceLength).toFixed(0))

forEach()

let books = [
    { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
    { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
    { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
    { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
    { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
    { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
 ]


let TotalPrice = 0;
let romanPriceLength = 0;

 books.forEach(book => {
  if((book.type).toLowerCase() === "roman"){
    TotalPrice += book.price
    romanPriceLength ++
  }
 })
 
 console.log(TotalPrice / romanPriceLength)
 console.log((TotalPrice / romanPriceLength ).toFixed(0))

for ... of

let books = [
    { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
    { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
    { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
    { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
    { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
    { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
 ]

let romanPriceLength = 0;
let TotalPrice = 0;

for(let book of books){
  if(book.type.toLowerCase() === "roman"){
    TotalPrice += book.price
    romanPriceLength++
  }
}

console.log(TotalPrice / romanPriceLength)
console.log((TotalPrice / romanPriceLength).toFixed(0))

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do it with filter and reduce too :

let books = [
    { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
    { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
    { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
    { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
    { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
    { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]

const romans = books.filter(book => book.type.toLowerCase() == 'roman') 
const romansTotalPrice = romans.reduce((a, {price}) => a + price, 0)
const romansAvaragePrice = (romansTotalPrice / romans.length).toFixed(2);
console.log(romansAvaragePrice)

about 4 years ago · Juan Pablo Isaza Relatório

0

Use a for loop or forEach() to get the sum of prices and the count.

let books = [
    { name: 'Some-1', author: 'Puskin', type: 'roman', price: 204.5 },
    { name: 'Some-2', author: 'Tolstoy', type: 'ROMAN', price: 321.0 },
    { name: 'Some-3', author: 'Pero', type: 'tale', price: 211.0 },
    { name: 'Some-4', author: 'Koelyo', type: 'roman', price: 204.5 },
    { name: 'Some-5', author: 'Christie', type: 'Roman', price: 245.5 },
    { name: 'Some-6', author: 'Hoho', type: 'tale', price: 366.0 }
]

let TotalPrice = 0;
let count = 0;

books.forEach(book => {
  if (book.type.toLowerCase() == 'roman') {
    TotalPrice += book.price;
    count++;
  }
});

console.log((TotalPrice / books.length).toFixed(2))

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