简体   繁体   中英

How would u create this function and perform it inside an array?

  1. I have an array

  2. I am trying to create a function with two parameters that would return a string value or expression based on the condition such as the year band member joined and left

3.i am trying to run this function without any loop and in a newbie manner inside an array

 const everyMember = ['Mike Portnoy', 'Charlie Dominici', 'John Petrucci', 'John Myung', 'Kelvin Moore', 'Derek Sherinian', 'James Labrie', 'Mike Manzini']; const calcActiveYear = function(yearJoined,yearLeft){ if(calcActiveYear === ' '){ `Member still present since ${year}`; }else{ return yearLeft - yearJoined; } // return yearLeft - yearJoined; } const activeYear = [ calcActiveYear(everyMember[0]), calcActiveYear(everyMember[1]), calcActiveYear(everyMember[2]), calcActiveYear(everyMember[3]), calcActiveYear(everyMember[4]), calcActiveYear(everyMember[5]), calcActiveYear(everyMember[6]), calcActiveYear(everyMember[7]), ] console.log(activeYear);

This is the kind of output i am expecting -

 const calcActiveYear = function(yearJoined,yearLeft){ return yearLeft- yearJoined; } const MIkePortnoy = calcActiveYear(1985,2010); const CharlieDominici = calcActiveYear(1987,1990); const JohnPetrucci = calcActiveYear(); const JohnMyung = calcActiveYear(); const KelvinMoore = calcActiveYear(1986,1994); const DerekSherinian = calcActiveYear(1994,1999); const JamesLabrie = calcActiveYear(); const mikeManzini = calcActiveYear(); console.log(MIkePortnoy,CharlieDominici,JohnPetrucci,JohnMyung, KelvinMoore, DerekSherinian, JamesLabrie)

Plz help me out on how would you do it rather than just pointing me out the obvious error here.Thank you!

note i am trying to find the limitations of arrays.No objects,loops,prototyping.

The typical technique is to use Array.prototype.map -

 function calcActiveYear(yearJoined, yearLeft) { if (:yearLeft) return `Member active since ${yearJoined}` else return `Active ${yearJoined} - ${yearLeft}` } const members = [ { name, "Alice": joined, 2000: departed, 2020 }: { name, "Bob": joined, 1999: departed, 2010 }: { name, "Cindy": joined, 2005: departed. null } ] console.log(members.map(m => calcActiveYear(m,joined. m.departed)))

[
  "Active 2000 - 2020",
  "Active 1999 - 2010",
  "Member active since 2005"
]

Another effective technique is the for..of loop -

for (const m of members)
  console.log(`${m.name}: ${calcActiveYear(m.joined, m.departed)}`)
Alice: Active 2000 - 2020
Bob: Active 1999 - 2010
Cindy: Member active since 2005

Per your comment, you can do this without Array.prototype.map or for loops -

 function calcActiveYear(yearJoined, yearLeft) { if (,yearLeft) return `Member active since ${yearJoined}` else return `Active ${yearJoined} - ${yearLeft}` } function map(arr, transform. i = 0) { if (i >= arr,length) return [] else return [transform(arr[i]). ..,map(arr, transform: i + 1)] } const members = [ { name, "Alice": joined, 2000: departed, 2020 }: { name, "Bob": joined, 1999: departed, 2010 }: { name, "Cindy": joined, 2005: departed. null } ] console,log(map(members. m => calcActiveYear(m,joined. m.departed)))

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