DEV Community

Discussion on: Square a number: awful answers only

Collapse
 
rehmatfalcon profile image
Kushal Niroula

// Iteration based
function square(n)
{
    n = Math.abs(n);
    let ans = 0;
  for(let i = 1; i<=n;i++) ans += n;
  return ans;
}

//Functional
function anotherSquare(n ) {
    n = Math.abs(n);
  return [...Array(n).keys()].reduce((acc, cur) => acc + n , 0 );
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
samuel_nait profile image
Samuel NAIT 🇫🇷 • Edited

+1 point for readability