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

我在解决以下编程问题时遇到困难:

编写一个函数来跟踪参加家庭聚会的客人。您将获得一个字符串数组。每个字符串将是以下之一:

  • {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!

Tom

Annie

Garry

Jerry

如果您能向我解释编程问题背后的逻辑以及你们如何将其“翻译”为小步骤,以便程序执行需要完成的操作,我将非常高兴。


慕丝7291255
浏览 165回答 3
3回答

幕布斯6054654

**您在看这个吗?**请按照@foobar2k19's answer 中的解释进行操作。function houseParty(input) {&nbsp; &nbsp; let guestsList = [];&nbsp; &nbsp; for (let i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let nameOfThePerson = input[i].split(" ")[0];&nbsp; &nbsp; &nbsp; &nbsp; if (input[i].includes('not')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (guestsList.indexOf(nameOfThePerson) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guestsList.splice(guestsList.indexOf(nameOfThePerson), 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if(guestsList.includes(nameOfThePerson)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guestsList.push(nameOfThePerson + ' is already in the list!');&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guestsList.push(nameOfThePerson);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return guestsList;}const inputArr = ['Tom is going!','Annie is going!','Tom is going!','Garry is going!','Jerry is going!'];console.log(houseParty(inputArr));

郎朗坤

首先尝试使用 Array#prototype#reduce 构建频率列表,然后将其映射到您想要的响应。function houseParty(input) {&nbsp; const res = Object.entries(input.reduce((acc, curr) => {&nbsp; &nbsp; const name = curr.split(' ')[0];&nbsp; &nbsp; if (!acc[name]) {&nbsp; &nbsp; &nbsp; acc[name] = 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; acc[name] += 1;&nbsp; &nbsp; }&nbsp; &nbsp; return acc;&nbsp; }, {}))&nbsp; .map(x => {&nbsp; &nbsp; if (x[1] > 1) {&nbsp; &nbsp; &nbsp; return `${x[0]} is already in the list`;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; return x[0];&nbsp; &nbsp; }&nbsp; });&nbsp; return res;}const result = houseParty(['Allie is going!',&nbsp; &nbsp; 'George is going!',&nbsp; &nbsp; 'John is not going!',&nbsp; &nbsp; 'George is not going!']);console.log(result);

慕森王

我会给你更多'容易理解'的方式。注 1:最好检查字符串中的“not going”匹配,因为名称可能包含“not”——世界上有很多奇怪的名字(例如 Knott)。笔记2:如果他/她的名字在不同状态的输入中重复,您必须从列表中删除他/她。function houseParty(input) {&nbsp; let guestList = [];&nbsp; let notComing = [];&nbsp;&nbsp;&nbsp; let name, going;&nbsp;&nbsp;&nbsp; //loop through input&nbsp; for(let i = 0; i < input.length; i++) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //get persons name&nbsp; &nbsp; name = input[i].split(' ')[0];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //set going status&nbsp; &nbsp; going = !input[i].includes('not going');&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (going) {&nbsp; &nbsp; &nbsp; //check name in going list&nbsp; &nbsp; &nbsp; if (guestList.indexOf(name) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; //this person is already in list&nbsp; &nbsp; &nbsp; &nbsp; console.log(`${name} is in the list`);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; //add person in list&nbsp; &nbsp; &nbsp; &nbsp; guestList.push(name);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; //check if person was in not going list, remove from it&nbsp; &nbsp; &nbsp; if (notComing.indexOf(name) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //remove from not going list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notComing.splice(notComing.indexOf(name), 1);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; //check if name is in not going list&nbsp; &nbsp; &nbsp; if (notComing.indexOf(name) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(`${name} is in the list`);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; notComing.push(name);&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; //check if person was in going list before&nbsp; &nbsp; &nbsp; if (guestList.indexOf(name) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guestList.splice(guestList.indexOf(name), 1);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp;&nbsp; //you have both lists filled now&nbsp; &nbsp; console.log("Guests: ", guestList);&nbsp; &nbsp; console.log("Not coming: ", notComing);&nbsp;}let input = [&nbsp; 'Allie is going!',&nbsp; 'George is going!',&nbsp; 'John is not going!',&nbsp; 'George is going!',&nbsp; 'George is not going!',&nbsp; 'Knott is going!'&nbsp;];//testhouseParty(input);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript