第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過斷點而不是多個 else if 語句查找索引

通過斷點而不是多個 else if 語句查找索引

慕慕森 2022-10-08 17:02:16
我正在嘗試根據(jù)人的身高生成尺寸。目前我的程序可以工作,但我想找到一種更短的方法來獲取大小,而不是使用所有這些else if語句。我想循環(huán)通過“斷點”來找到相應的索引。這是我的原始代碼+我的想法。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];
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經(jīng)驗 獲得超6個贊

 const sizes = ['xxs', 'xxs or xs', 'xs', 'xs or s', "even another one here"]

// Goes on for all sizes...


function generateSize(height){

    let size;

    let left = 142;

    let right = 142;

    // Initialize the variables for comparison

    // Left and right comparison based on position of "&"

    // This is just user-defined

    for(var i = 0; i < sizes.length; i++){

        if(height < right){

          size = sizes[i];

          return size;

        }

        else {

            // add counter from here

            // This takes us to the next item in the array

            i += 1;

            // add right comparison with intervals of 6

            right += 6;

            if(height >= left && height < right){

                size = sizes[i];

                return size;

            }

            else {

              // add left comparison with intervals of 6

              left += 6;

              // revert the counter to its initial value

              i -= 1;

            }

        }

    }

}

console.log("First: " + generateSize(141))

console.log("Second: " + generateSize(147))

console.log("Third: " + generateSize(153))

console.log("Fourth: " + generateSize(159))

console.log("Last: " + generateSize(161));

// Note this 161, which will return the new last value in the array

這假設您的大小間隔為 6,(它們是)并返回與數(shù)組對應的相應值



查看完整回答
反對 回復 2022-10-08
?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

我認為你可以通過調整你的起始數(shù)據(jù)結構來大大簡化這一點。如果我們有一個將大小及其斷點聯(lián)系在一起的對象數(shù)組怎么辦:


const sizeMap = [

    { maxHeight: 142, size: 'xxs' },

    { maxHeight: 148, size: 'xxs or xs' },

    { maxHeight: 154, size: 'xs' },

    { maxHeight: 160, size: 'xs or s' },

]


const getSize = height => sizeMap.find(item => height < item.maxHeight).size


console.log(getSize(143))

數(shù)組函數(shù)find返回滿足您條件的第一個值。這種方法工作的先決條件是讓您的數(shù)組對象的高度按升序排列。


查看完整回答
反對 回復 2022-10-08
?
牧羊人nacy

TA貢獻1862條經(jīng)驗 獲得超7個贊

if(height<160){

height-=142;

if(height<0){size=sizes[0]}

else{

size=sizes[(hieght)%6]

}

}

else{

size='larger...'

}

檢查這是否適用于所有情況我困了


查看完整回答
反對 回復 2022-10-08
  • 3 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號