Do something x times

PHOTO EMBED

Tue Aug 09 2022 19:28:02 GMT+0000 (Coordinated Universal Time)

Saved by @johnnye562 #javascript

const times = (times, func) => {
  if (isNaN(times)) {
    console.error("times to run must be specified")
    return
  }
  if (typeof func !== "function") {
    console.error(`func must be a valid function, ${typeof func} provided`)
    return
  }
  Array.from(Array(times)).forEach(() => {
    func()
  })
}

times(3, () => console.log("hello"))
content_copyCOPY

https://justacoding.blog/9-useful-javascript-utility-functions/