jobbet litt med arrow functions

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-23 12:41:34 +02:00
parent 81f3550ab2
commit 98723969eb
2 changed files with 31 additions and 0 deletions

View file

@ -0,0 +1,20 @@
const SCRIPTS = require("./scripts.js");
function reduce(array, combine, start) {
let current = start;
for(let element of array){
current = combine(current, element);
}
return current
}
console.log(reduce([1,2,3,4], (a,b) => a +b, 0));
console.log([1,2,3,4].reduce((a,b) => a +b));
function characterCount(script) {
return script.ranges.reduce((count, [from, to]) => {
return count + (to - from);
}, 0);
}
console.log(SCRIPTS.reduce((a,b) => {
return characterCount(a) < characterCount(b) ? b : a;
}));

View file

@ -0,0 +1,11 @@
const SCRIPTS = require("./scripts.js");
function map(array, transform) {
let mapped = [];
for (let element of array){
mapped.push(transform(element));
}
return mapped
}
let rtlScripts = SCRIPTS.filter(s => s.direction == 'rtl');
console.log(map(rtlScripts, s => s.name));