Ad
Algorithms
Logic
Arrays
Data Types
Mathematics
Numbers
Statistics
Data
Code
Diff
  • function mad(numbers) {
      if (!numbers.length) return null;
      const mean = numbers.reduce((mean, number) => mean + number) / numbers.length;
      return (
        numbers.reduce((mad, number) => mad + Math.abs(number - mean), 0) /
        numbers.length
      );
    }
    
    • function mad(a) {
    • if (!a.length) return null;
    • let n = a.reduce((x, y) => x + y, 0) / a.length;
    • return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;
    • }
    • function mad(numbers) {
    • if (!numbers.length) return null;
    • const mean = numbers.reduce((mean, number) => mean + number) / numbers.length;
    • return (
    • numbers.reduce((mad, number) => mad + Math.abs(number - mean), 0) /
    • numbers.length
    • );
    • }

Given an array of numbers, move all the zeros to the end of the array without changing the order of the non-zero numbers in the array.

E.g.

[1,0,2,0,3,0,4,5] --> [1,2,3,4,5,0,0,0]
Code
Diff
  • const moveZeros = (arr) => [...arr.filter(n => n), ...arr.filter(n => !n)];
    • var moveZeros = function (arr) {
    • return arr.filter(el => el !== 0).concat(arr.filter(el => el === 0));
    • }
    • const moveZeros = (arr) => [...arr.filter(n => n), ...arr.filter(n => !n)];