我正在尝试根据人的身高生成尺寸。目前我的程序可以工作,但我想找到一种更短的方法来获取大小,而不是使用所有这些else if语句。我想循环通过“断点”来找到相应的索引。
这是我的原始代码+我的想法。
const sizes = ['xxs', 'xxs or xs', 'xs', 'xs or s'] // Goes on for all sizes...
function generateSize(height) {
let size;
if (height < 142) {
size = sizes[0];
} else if (height >= 142 && height < 148) {
size = sizes[1];
} else if (height >= 148 && height < 154) {
size = sizes[2];
} else if (height >= 154 && height < 160) {
size = sizes[3]; // Goes on for all sizes...
} else {
size = 'larger...';
}
return size;
}
// Example of what I had in mind.
const heightBreakpoints = [142, 148, 154, 160];
function getByBreakpoints(breakpoints, height){ // Part where I am stuck.
let index;
// Loop through breakpoints...
return index;
}
const sizeIndex = (getByBreakpoints(heightBreakpoints, 158));
const s = sizes[sizeIndex];
跃然一笑
慕桂英546537
牧羊人nacy
相关分类