为了账号安全,请及时绑定邮箱和手机立即绑定

用数组方法解决编程问题?

用数组方法解决编程问题?

慕丝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 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信