0

I have one JS file called taxForms.js and a script syntax within the html file. And I need to make changes within the html file, so the console.log() puts out the right output. My task goes like this:

In income-greather-than-500k.html you will find an array of tax forms assigned to the variable called taxForms. In its current state the entire array is logged to the console. You need to change the Javascript in incomegreather-than-500k.html such that the array is iterated and only the real name of the superheroes that have an income greater than 500 000 are logged with console.log.

I have tried if statements to output the right answer, but I always get an error in the console.

javaScript:

const taxForms = [
  {
    realName: "Bruce Wayne",
    income: 750000,
    wealth: 300000
  },
  {
    realName: "John Blake",
    income: 440000,
    wealth: 832000
  },
  {
    realName: "Selina Kyle",
    income: 640000,
    wealth: 432000
  }
];

html:

<!DOCTYPE html>
<html>
  <head>
    <title>Income greather than 500 000</title>
    <meta charset="UTF-8">
    <script src="taxForms.js"></script>
  </head>
  <body>
    <script>
      if (taxForms[income] > 500000) {
        console.log(taxForms);
      }
    </script>
  </body>
</html>
Gabriel
  • 21
  • 4
  • One of the instructions reads " You need to change the Javascript in incomegreather-than-500k.html such that the array is iterated...". This means using a for loop or an Array method such `forEach` to process array entries. – traktor Nov 04 '22 at 16:09

2 Answers2

1

taxForms[income] is undefined. Iterate the array instead:

for (const taxForm of taxForms) {
  if (taxForm.income > 500000) {
    console.log(taxForms)
  }
}

And I recommend to change

<script src="taxForms.js"></script>

to

<script src="taxForms.js" defer></script>

Docs on defer attribute here.

ericmp
  • 1,163
  • 4
  • 18
0

You are getting an error, because, you're trying to console.log array of objects. To get the solution you are going to need to loop through the array and console.log the specific object in this array.

For (let i=0; i<taxForms.length; i++){ if (taxForms[i][income]>500000){ console.log(taxForms[i][realName] }}

Tony
  • 14
  • 4