使数组中的数字相对于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 生日广州男生祝福语简短