亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

用數組方法解決編程問題?

用數組方法解決編程問題?

慕絲7291255 2021-12-02 15:02:26
我在解決以下編程問題時遇到困難:編寫一個函數來跟蹤參加家庭聚會的客人。您將獲得一個字符串數組。每個字符串將是以下之一:{name} 出發了!{name} 不去!如果您收到第一種類型的輸入,如果他/她不在列表中,則必須添加該人(如果他/她在列表中,則打印:{name} 已在列表中!如果您收到第二種類型的輸入,如果他/她在列表中,你必須刪除他/她(如果沒有,打?。簕name} 不在列表中!)。最后將所有客人打印在單獨的行上。任務是使用數組方法、for 循環、for each、for of...任何可行的方法來解決它。我知道這對這個網站來說可能太簡單了,我很抱歉,但我已經為此苦苦掙扎了太多個小時,不幸的是,這是我可以使用的代碼……我的問題是我似乎無法將其分成小步驟并使用數組方法和循環執行它們...function houseParty(input) {    let guestsList = [];    let notComing = [];    for (let i = 0; i < input.length; i++) {        if (input[i].includes('not')) {            notComing.push(input[i]);        } else {            guestsList.push(input[i]);        }    }}houseParty(['Allie is going!',    'George is going!',    'John is not going!',    'George is not going!'])這是一個輸入示例:[Tom is going!,Annie is going!,Tom is going!,Garry is going!,Jerry is going!]這是預期的輸出:Tom is already in the list!TomAnnieGarryJerry如果您能向我解釋編程問題背后的邏輯以及你們如何將其“翻譯”為小步驟,以便程序執行需要完成的操作,我將非常高興。
查看完整描述

3 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

**您在看這個嗎?**請按照@foobar2k19's answer 中的解釋進行操作。


function houseParty(input) {


    let guestsList = [];


    for (let i = 0; i < input.length; i++) {

        let nameOfThePerson = input[i].split(" ")[0];

        if (input[i].includes('not')) {

            if (guestsList.indexOf(nameOfThePerson) > -1) {

                guestsList.splice(guestsList.indexOf(nameOfThePerson), 1);

            }

        } else if(guestsList.includes(nameOfThePerson)) {

            guestsList.push(nameOfThePerson + ' is already in the list!');

        } else {

            guestsList.push(nameOfThePerson);

        }

    }

    return guestsList;

}


const inputArr = ['Tom is going!',

'Annie is going!',

'Tom is going!',

'Garry is going!',

'Jerry is going!'];


console.log(houseParty(inputArr));


查看完整回答
反對 回復 2021-12-02
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

首先嘗試使用 Array#prototype#reduce 構建頻率列表,然后將其映射到您想要的響應。


function houseParty(input) {

  const res = Object.entries(input.reduce((acc, curr) => {

    const name = curr.split(' ')[0];

    if (!acc[name]) {

      acc[name] = 1;

    } else {

      acc[name] += 1;

    }


    return acc;

  }, {}))

  .map(x => {

    if (x[1] > 1) {

      return `${x[0]} is already in the list`;

    } else {

      return x[0];

    }

  });

  return res;

}


const result = houseParty(['Allie is going!',

    'George is going!',

    'John is not going!',

    'George is not going!'

]);


console.log(result);


查看完整回答
反對 回復 2021-12-02
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

我會給你更多'容易理解'的方式。


注 1:


最好檢查字符串中的“not going”匹配,因為名稱可能包含“not”——世界上有很多奇怪的名字(例如 Knott)。


筆記2:


如果他/她的名字在不同狀態的輸入中重復,您必須從列表中刪除他/她。


function houseParty(input) {

  let guestList = [];

  let notComing = [];

  

  let name, going;

  

  //loop through input

  for(let i = 0; i < input.length; i++) {

    

    //get persons name

    name = input[i].split(' ')[0];

    

    //set going status

    going = !input[i].includes('not going'); 

    

    

    if (going) {

      //check name in going list

      if (guestList.indexOf(name) > -1) {

        //this person is already in list

        console.log(`${name} is in the list`);

      }

      else {

        //add person in list

        guestList.push(name);

      }

      

      //check if person was in not going list, remove from it

      if (notComing.indexOf(name) > -1) {

          //remove from not going list

          notComing.splice(notComing.indexOf(name), 1);

      }

    }

    else {

      //check if name is in not going list

      if (notComing.indexOf(name) > -1) {

        console.log(`${name} is in the list`);

      }

      else {

        notComing.push(name); 

      }

      

      //check if person was in going list before

      if (guestList.indexOf(name) > -1) {

          guestList.splice(guestList.indexOf(name), 1);

      }

    }

  }

  

  //you have both lists filled now

    console.log("Guests: ", guestList);

    console.log("Not coming: ", notComing); 

}


let input = [

  'Allie is going!',

  'George is going!',

  'John is not going!',

  'George is going!',

  'George is not going!',

  'Knott is going!' 

];



//test

houseParty(input);


查看完整回答
反對 回復 2021-12-02
  • 3 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號