18 May 2023

Write a Function that Returns the Number of Elements where it's Callback is True

Taken from Execute Program

Write a function that takes an array and a callback function, then returns the number of elements where callback returns true. Use .forEach to iterate through the array.


This is the answer I came up with:

// typescript

// Function type (takes in param of any type, returns any type)
type Callback = (param: any) => any

function count(arr: Array<any>, callback: Callback) {
  let count = 0
  arr.forEach(element => {
    if (callback(element)) {
      count += 1
  return count


Solution tested in REPL:


Webmentions & Comments

Copyright © Paramdeo Singh · Built with Jekyll in 🇬🇾 · All Rights Reserved

[ this node is permanently morphing last updated on 28 May 2023 ]

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems