Coder Social home page Coder Social logo

js-arrary-methods's Introduction

JS forEach(), map(), and filter()

.forEach()

const array1 = ['a', 'b', 'c'];

array1.forEach(function(element){
  console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

.map()

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(function(x){
	return x * 2;
});

console.log(map1);
// expected output: Array [2, 8, 18, 32]```

## `.filter()` 
```js
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(
function(word){
	return word.length > 6
});

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]```


## Squares

```js
const nums = [1, 2, 3, 4, 5];
let numsSquared = [];

// Use .map() to get the square of nums and store the value in numsSquared
// numsSquared => [1, 4, 9, 16, 25]

isDivisibleBy3

const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let isDivisibleBy3 = [];

// Use .filter() to find out the numbers that are divisible by 3 and store the value in isDivisibleBy3
// isDivisibleBy3 => [3, 6, 9]

Abbreviations

const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let dayAbbreviations = [];

// Find the abbreviation of all days and add them to dayAbbreviations array
// dayAbbreviations => ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

Capitalize

const instructors = ['ahmed', 'maha', 'sami', 'salman'];
let capitalizedInstructors = [];

// Capitalize all the strings in the instructors array and store them in the array capitalizedInstructors.
// capitalizedInstructors => ["AHMED", "MAHA", "SAMI", "SALMAN"]

Crazy numbers, again!

// These crazy numbers are now strings ๐Ÿ˜ฏ ๐Ÿ˜ฏ  !!  
const stringNumbers = ["103440", "3799.2663", "3.14159265359", "859494", "59439"];
let totalNumbersUnder4000 = 0;

// Convert numbers from strings to numbers and sum all numbers under 4000 and store them in totalNumbersUnder4000
// totalNumbersUnder4000 => 3802.4078926536

js-arrary-methods's People

Contributors

salman778 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.