Quick actions

cmd+k|ctrl+k

Navigation

Languages

JS Reusable Function

Snippet info

Language

JavaScript

Visibility

public

Author

nacht777

Created

2022-09-30T07:04:52.701667Z

Updated

2022-09-30T07:28:27.696312Z

//Array Map
const newArray = ['Harry', 'Ron', 'Jeff', 'Thomas'].map((name) => { return `${name}!`});//callback function

console.log(newArray);

//Array Filter
const truthyArray = [1, '', 'Hallo', 0, null, 'Harry', 14].filter((item) => Boolean(item));

console.log(truthyArray);

const students = [
  {
    name: 'Harry',
    score: 60,
  },
  {
    name: 'James',
    score: 88,
  },
  {
    name: 'Ron',
    score: 90,
  },
  {
    name: 'Bethy',
    score: 75,
  }
];

const eligibleForScholarshipStudents = students.filter((student) => student.score > 85);

console.log(eligibleForScholarshipStudents);

//Array Reduce
const murids = [
  {
    name: 'Harry',
    score: 80,
  },
  {
    name: 'James',
    score: 88,
  },
  {
    name: 'Ron',
    score: 90,
  },
  {
    name: 'Bethy',
    score: 75,
  }
];

const totalScore = students.reduce((acc, murid) => acc + murid.score, 0);

console.log(totalScore);

//Array Some
const array = [1, 3, 5];
const even = array.some(element => element % 2 === 0);

console.log(even);

//Array Sort
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// output: [ 'Dec', 'Feb', 'Jan', 'March' ];

const array1 = [1, 30, 4, 1000, 101, 121];
array1.sort();
console.log(array1);
// output: [ 1, 1000, 101, 121, 30, 4 ]

const array2 = [1, 30, 4, 1000];

const compareNumber = (a, b) => {
  return a - b;
};
const sorting = array2.sort(compareNumber);
console.log(sorting);

//Array Every
const scores = [70,85,90];
const minimumScore = 70;

const examPassed = scores.every(score => score >= minimumScore);
console.log(examPassed);

//Array ForEach
INFO