-1

How can i edit the following cord to display the total number of impression and conversion. when i console.log(data) i get array(100000) your assistance will be highly appreciated.

<script>
    
//pulling data from local json file
fetch('./logs.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    // console.log(data);
    let data1 = "";

    data.forEach((values) => {
      data1 += ` 
            <div class="card">
            <p class="impression">${values.impression}</p>
            <p class="conversion">${values.conversion}</p>
            </div> `;
    });

    document.querySelector("#card").innerHTML = data1;

  })
  .catch((error) => {
    console.log(error);
  })
    
</script>
3
  • can you please show me through editing my code on doing it Commented Sep 9, 2022 at 18:21
  • i have tried the code but still nothing different Commented Sep 9, 2022 at 18:43
  • I showed how to do it an hour ago. If you have an issue with it, you should comment on the answer, not the question. Commented Sep 9, 2022 at 19:20

1 Answer 1

1

Add total variables to the code, and increment them in the forEach loop.

fetch('./logs.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    // console.log(data);
    let data1 = "";
    let totalImpressions = 0;
    let totalConversions = 0;
    data.forEach((values) => {
      totalImpressions += values.impression;
      totalConversions += values.conversion;
      data1 += ` 
            <div class="card">
            <p class="impression">${values.impression}</p>
            <p class="conversion">${values.conversion}</p>
            </div> `;
    });
    data1 += `<div>Total impressions = ${totalImpressions} Total conversions = ${totalConversions}</div>`;
    document.querySelector("#card").innerHTML = data1;

  })
  .catch((error) => {
    console.log(error);
  })

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried running the code you suggested but still no information is being displayed on my html card yet there is no error message in my console
You might need to display the totals in another div instead of #card. But the basic logic should still work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.