简体   繁体   中英

How to write a Javascript function that returns objects inside an array as outputs

Good day all. I have this short task I am supposed to work on. It has two parts. I have done the first part (To the best of my ability, but I'm open to better ways of doing it), while the second is giving me problems. The task is shown below:

  • First create an array of objects called data with the following values:

    1. Principal- 2500, time- 1.8
    2. Principal- 1000, time- 5
    3. Principal- 3000, time- 1
    4. Principal- 2000, time- 3

NB: Each individual object should have 'principal' and 'time' as keys. 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;

Calculate the interest for each individual object using the formula:

(principal * rate * time) / 100.

The function should return an array of objects called interestData and each individual object should have 'principal', 'rate', 'time' and 'interest' as keys with their corresponding values. Log the 'interestData' array to console BEFORE your return statement. Finally, call/execute the function and pass the 'data' array you created.*

How do I return an object from the function with the keys?

This is what I have tried already

 const objArr = [{ "principal": 2500, "time": 1.8 }, { "principal": 1000, "time": 5 }, { "principal": 3000, "time": 1 }, { "principal": 2000, "time": 3 } ] //console.log(objArr.length) function interestCalculator(array) { let rate = 0; let interestData = []; array.forEach(function(entry) { if (entry.principal >= 2500) { if (entry.time > 1.5 && entry.time < 3) { rate = 3; } else if (entry.time >= 3) { rate = 4; } } else if (entry.principal < 2500 || entry.time <= 1) { rate = 2; } else { rate = 1; } const interest = (entry.principal * rate * entry.time) / 100; interestData.push(entry.principal, rate, entry.time, interest); //return interest; }) console.log(interestData.length); } interestCalculator(objArr);

Thanks in advance.

To build an object use the notation {key: value, key2: value2, ...}

interestData.push(
  {
    "principal": entry.principal, 
    "rate": rate, 
    "time": entry.time, 
    "interest": interest
  }
);

 const objArr = [{"principal": 2500, "time": 1.8}, {"principal": 1000, "time": 5}, {"principal": 3000, "time": 1}, {"principal": 2000, "time": 3}] function interestCalculator(array) { let rate = 0; let interestData = []; array.forEach(function(entry) { if (entry.principal >= 2500) { if (entry.time > 1.5 && entry.time < 3) { rate = 3; } else if (entry.time >= 3) { rate = 4; } } else if (entry.principal < 2500 || entry.time <= 1) { rate = 2; } else { rate = 1; } const interest = (entry.principal * rate * entry.time) / 100; interestData.push({ "principal": entry.principal, "rate": rate, "time": entry.time, "interest": interest }); }) console.log(interestData); } interestCalculator(objArr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM