使数组中的数字相对于JavaScript中的0 – 100
假设我们有一个包含一些数字的数组,我们的工作是编写一个函数,该函数接受该数组并将所有与0到100相关的值映射。而所有其他数字都应根据该比率转换为0到100之间的特定数字。
以下是执行相同操作的代码-
示例
const numbers = [45.71, 49.53, 18.5, 8.38, 38.43, 28.44];
const mapNumbers = (arr) => {
const max = Math.max(...arr);
const min = Math.min(...arr);
const diff = max - min;
return arr.reduce((acc, val) => acc.concat((100/diff)*(val-min)), []);
};
console.log(mapNumbers(numbers));输出结果
控制台中的输出将为-
[ 90.71688942891859, 100, 24.59295261239368, 0, 73.02551640340218, 48.74848116646417 ]
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志