0
let data = 
        [
            {
                principal: 2500,
                time: 1.8
            },
            {
                principal: 1000,
                time: 5
            },
            {
                principal: 3000,
                time: 1
            },
            {
                principal: 2000,
                time: 3
            }
        ]
    console.log(data)

    function interestCalculator() {
        for (let i of data)

        if (data[i].principal>=2500 && data[i].time>1 && data[i].time<3){

        return "rate = 3"
        } else if (data[i].principal>=2500 && data[i].time>=3){

        return "rate = 4"
        } else if (data[i].principal<2500 && data[i].time<=1 ){

        return "rate = 2"
        } else {

        return "rate = 1"
        }

    }

I'm trying to write a function called "interestCalculator" that takes an array as a single argument and does the following:

Determine the rate applicable using the conditions:

  • If the principal is greater than or equal to 2500 and the time is greater than 1 and less than 3, then rate = 3

  • If the principal is greater than or equal to 2500 and the time is greater than or equal to 3, then rate = 4

  • If the principal is less than 2500 or the time is less than or equal to 1, then rate = 2

  • Otherwise, rate = 1;

But then I keep getting errors saying principal is undefined

matthias_h
  • 11,356
  • 9
  • 22
  • 40
RandWuch
  • 13
  • 5

4 Answers4

0

instead of data[i].principial, you should write like i.principial. Here i is not index. it is just an element from the array.

Kid
  • 1,160
  • 2
  • 14
  • 31
0

when you loop over the array

for (let i of data) { ... }

i is already the object, so you need to use i.principal and i.time.


If you want instead to access the ith element with data[i] then you need to loop the array with

 for (let i = 0, j = data.length; i++; i < j) { ... }

Finally as a side note it's worth to point that you have some return statements inside the loop, so the function will just check the first object in the array and will soon return, no matter how many elements may have data.

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
0

Please update loop statement. Use in instead of of

for (let i in data)
0

you didnt have braces around your for loop and "for of" returns objects (ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)

function interestCalculator() {
    for (let obj of data) {   
        if (obj.principal>=2500 && obj.time>1 && obj.time<3){
           return "rate = 3"
        } 
        else if (obj.principal>=2500 && obj.time>=3){    
           return "rate = 4"
        } 
        else if (obj.principal<2500 && obj.time<=1 ){    
            return "rate = 2"
        } 

        return "rate = 1"            
    }
}
developer
  • 690
  • 7
  • 16
  • @RandWuch also note in your code the last else in your code is redundant, as you are returning anyway, so i have removed in mine – developer Apr 07 '20 at 15:21