From 9835ea447159f352b7830bea7551d2ef30372445 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Tue, 24 Sep 2024 08:59:47 +0200 Subject: [PATCH] jobbet litt med higher order functions --- .../summarizing_with_reduce.js | 81 +++++++++++++++++-- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/eloquentjavascript/05_higher_order_functions/summarizing_with_reduce.js b/eloquentjavascript/05_higher_order_functions/summarizing_with_reduce.js index 3712418..be67631 100644 --- a/eloquentjavascript/05_higher_order_functions/summarizing_with_reduce.js +++ b/eloquentjavascript/05_higher_order_functions/summarizing_with_reduce.js @@ -2,19 +2,88 @@ const SCRIPTS = require("./scripts.js"); function reduce(array, combine, start) { let current = start; - for(let element of array){ + 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)); +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; -})); \ No newline at end of file +// console.log(SCRIPTS.reduce((a, b) => { +// return characterCount(a) < characterCount(b) ? b : a; +// })); +// let biggest = null; + +// for (let script of SCRIPTS) { +// if (biggest == null || +// characterCount(biggest) < characterCount(script)) { +// biggest = script; +// } +// } +// console.log(biggest); + +function average(array){ + return array.reduce((a,b) => a + b) / array.length; +} +// console.log(Math.round(average(SCRIPTS.filter(s => !s.living).map(s => s.year)))); + +let total = 0, count = 0; +for (let script of SCRIPTS){ + if(script.living) { + total += script.year; + count += 1; + } +} +// console.log(Math.round(total / count)); + +function characterScript(code){ + for (let script of SCRIPTS) { + if (script.ranges.some(([from, to]) => { + return code >= from && code < to; + })) { + return script + } + } + return null; +} +console.log(characterScript(1200)); +let roseDragon = "🌹🐉"; +for (let char of roseDragon){ + console.log(char); +} + +function countBy(items, groupName){ + let counts = []; + for (let item of items){ + let name = groupName(item); + let known = counts.find(c => c.name == name); + if (!known) { + counts.push({name, count: 1}); + } else { + known.count++; + } + } + return counts; +} +// console.log(countBy([1,2,3,4,5], n => n > 2)); + +function textScripts(text){ + let scripts = countBy(text, char => { + let script = characterScript(char.codePointAt(0)); + return script ? script.name : "none"; + }).filter(({name}) => name != "none"); + + let total = scripts.reduce((n, {count}) => n + count, 0); + if (total == 0){return "No scripts found!"}; + + return scripts.map(({name, count}) => { + return `${Math.round(count * 100 / total)}% ${name}`; + }).join(", "); +} +console.log(textScripts('英国的狗说"woof", 俄罗斯的狗说"тяв"')); \ No newline at end of file