Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate correctly

I try to calculate votes from my Dapp.
But I dont know how to calculate them this is what i have so fare

    useEffect(() => {
    const fetchAllVotes = async () => {
        const items = await fetchItems(); // get all the items
        const votesPerItem = await Promise.all(
            items.map(async (item) => {
                const votes = await getVotes(item.id); // get all the votes for the item
                return votes;
            })
        );
        const allVotes = votesPerItem.flat(); // combine all the votes into a single array
        setVotes(allVotes);
    };
    fetchAllVotes();
}, [])


votes.map((vote) => {
    console.log(vote.toString());
})

this is my console but how do i calculate the numbers

I also tried to reduce like this.. But then i get the result like the second picture indicates

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  const totalVotes = votes.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

console.log("Total votes:", totalVotes);

Then i get this

>Solution :

Looks like you are getting an array of string. Don’t forget to convert them into integer or float:

const sum = votes.reduce((partialSum, a) => parseInt(partialSum) + parseInt(a), 0);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading