css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Open your Console <kbd>Ctrl/Command + F12</kbd></h1>
            
          
!
            
              kbd {
  color: red;
  font-weight: bolder;
}
            
          
!
            
              /*
* @leandrw - I've implemented some of array 'functional functions' like slice, filter, map, reduce, forEach.
* The only one that I have to use the built-in was `sort`, the most complex of them.
* The results use `console.table` from devTools, so make sure to test in a browser that supports it.
*/
// ARRAY 1
const texasss = [
  {
    name: "Mike",
    age: 23,
    gender: "m",
    us: false
  },
  {
    name: "Liz",
    age: 20,
    gender: "f",
    us: true
  },
  {
    name: "Chris",
    age: 102,
    gender: "m",
    us: true
  },
  {
    name: "Chuloo",
    age: 27,
    gender: "m",
    us: false
  },
  {
    name: "Annie",
    age: 30,
    gender: "f",
    us: true
  }
];

// Part 1 - Find all users older than 24
console.log("all users older than 24");
console.table(filter(texasss, user => user.age < 24));

// Part 2 - Find the total age of all users
console.log(
  "total age of all users",
  reduce(texasss, (total, user) => total + user.age, 0)
);

// Part 3 - List all female coders
console.log("all female coders");
console.table(filter(texasss, user => user.gender === "f"));

// ARRAY 2
const newieyork = [
  {
    name: "Michelle",
    age: 19,
    coder: true,
    gender: "f",
    us: true
  },
  {
    name: "Sam",
    age: 25,
    coder: false,
    gender: "m",
    us: false
  },
  {
    name: "Ivy",
    age: 26,
    coder: true,
    gender: "f",
    us: false
  },
  {
    name: "Nick",
    age: 32,
    coder: true,
    gender: "m",
    us: true
  },
  {
    name: "Jim Beglin",
    age: 65,
    coder: false,
    gender: "m",
    us: true
  }
];

// Part 1 - List
console.log("all users in US in ascending order");
console.table(
  sort(filter(newieyork, user => user.us), (a, b) => a.name > b.name)
);

// Part 3 -  List all female coders
console.log("all female coders");
console.table(filter(newieyork, user => user.gender === "f" && user.coder));

// Part 2 - Sort all users by age
console.log("sort all users by age");
console.table(sort(newieyork, (a, b) => a.age - b.age));

// ARRAY 3
const vegzas = [
  {
    name: "Charly",
    age: 32,
    coder: true,
    gender: "m"
  },
  {
    name: "Law",
    age: 21,
    coder: true,
    gender: "m"
  },
  {
    name: "Rosey",
    age: 42,
    coder: false,
    gender: "f"
  },
  {
    name: "Steph",
    age: 18,
    coder: true,
    gender: "f"
  },
  {
    name: "Jon",
    age: 47,
    coder: false,
    gender: "m"
  }
];

// Part 1 - Find the total age of male coders under 25
console.log(
  "Find the total age of male coders under 25",
  reduce(
    filter(vegzas, user => user.age < 25 && user.gender === "m" && user.coder),
    (total, user) => total + user.age,
    0
  )
);

// Part 2 - List all male coders over 30
console.log("List all male coders over 30");
console.table(
  filter(vegzas, user => user.gender === "m" && user.coder && user.age > 30)
);

// Part 3 - Find the total age of everyone in texasss, newieyork and vegzas combined
console.log(
  "Find the total age of everyone in texasss, newieyork and vegzas combined",
  reduce(
    concat(texasss, newieyork, vegzas),
    (total, user) => total + user.age,
    0
  )
);

// Custom array functions 
function slice(arr, from, many){
  const newArr = [];
  
  from = from || 0;
  many = many || arr.length;
  
  for (let i = from, j = many; i < j; i++) {
    newArr.push(arr[i]);
  }
  
  return newArr;
}

function sort(arr, func) {
  // I've tried, but it seemed more complicated than I thought 
  return arr.sort(func);
}

function filter(arr, func) {
  const filtered = [];
  const cloneArr = slice(arr);

  forEach(cloneArr, (value, index, array) => {
    if (func(value, index, array)) {
      filtered.push(value);
    }
  });

  return filtered;
}

function map(arr, func) {
  const arrCloned = slice(arr);
  const mappedArr = [];

  forEach(arrCloned, (value, index, array) => {
    mappedArr.push(func(value, index, array));
  });

  return mappedArr;
}

function forEach(arr, func) {
  const cloneArr = slice(arr);

  for (let i = 0, j = cloneArr.length; i < j; i++) {
    func(cloneArr[i], i, cloneArr);
  }
}

function reduce(arr, func, start) {
  let total = start;
  const cloneArr = slice(arr);

  forEach(cloneArr, item => {
    total = func(total, item);
  });

  return total;
}

function concat(arr, ...arrays) {  
  const concatedArray = slice(arr);

  forEach(arrays, (array) => {
    forEach(array, (item) => {
      concatedArray.push(item);
    });
  });

  return concatedArray;
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console