删除和添加数组元素的问题

问题描述 投票:0回答:4

我有一个包含不同食物的数组,然后出现一个提示,您可以在其中选择其中一个,然后我想向他们展示他们在“grocery()”函数中选择的内容,但它只显示“完成”,因为这是我输入的最后一个单词,当我在提示中输入它时,是否有办法从第一个数组中取出该内容,并将其存储在一个新的数组变量中,然后显示它?我已经尽力了。

任何人都可以帮忙吗!我将不胜感激!

这是代码

function blue() {
    alert("You start walking on the blue road");
    alert("You spot a grocery store at the side of the road.");
    alert("You decide to walk inside, and buy yourself some food.")
    alert("You open the door to the store, you hear a loud bell as you opened the door.")
    alert("You notice the cashier turning her head towards you.")
    alert("You take a bag, and nods politely, then walks in to the store.")
    alert("Here's a list of all the things available in the store.")
    var inStock = ['apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'ham', 'carrot',
        'broccoli', 'pizza', 'crackers', 'onion', 'banana', 'frozen dinner', 'cereals'
    ];
    var search = prompt(inStock + " " + "Choose which things you want to buy, type 'done' when you are done." + " type one thing at the time.");

    while (inStock.indexOf(search) > -1) {
        alert("You grabbed a " + search + " and put it in the bag")
        search = prompt(inStock + " " + "Choose which things you want to buy, type 'done' when you are done." + " type one thing at the time.")
        if (search === "done") {
            grocery();

        }

    }

    function grocery() {
        alert("You decided that its enough")
        alert("In your bag you have " + search);
    }   

}     
javascript
4个回答
3
投票

那是因为这行:

alert("In your bag you have " + search);

它显示“完成”,因为您在消息中输入了“搜索”值。您要求用户输入“完成”,因此

search=="done"

您需要创建一些东西来保留输入的每个项目,例如另一个数组:

var inBag = [];

每次用户输入一个项目时,请像这样添加它:

inBag.push(search);

这是一个工作示例: https://jsfiddle.net/vknvLtzb/1/


1
投票

问题是您每次都不断将

search
重新分配给新值,而永远不会存储以前的值。

也有几种方法可以做到这一点。我的偏好是创建一个数组并将所有未完成的输入粘贴到该数组中。然后你可以用“,”将它们连接起来并得到一个很好的句子。

添加这一点:

var search = prompt(inStock + " " + "Choose wich things you wanna buy, type 'done' when you are done." + " type one thing at the time.");
var bag = []; //Shorthand for an empty array
bag.push(search); //adding your first selection to the bag array

这声明了一个数组,因为 search 只是一个字符串值。这个

bag
数组可用于保存您的所有物品。由于您使用的循环有点不确定,我们必须立即将第一个搜索项添加到包中。

然后,将“如果完成”更改为:

if (search === "done") {
    grocery();
}else{
     bag.push(search);   
}

这会检查每次尝试,如果未“完成”,则会将其添加到包中。否则它会调用你的函数。

然后将最后一点更改为:

alert("In your bag you have " + bag.join(", "));

join
将数组中的所有元素组合成一个字符串,使用带引号的位作为“粘合剂”。在这种情况下,这是一个很好的句子。

这是一个正在工作的小提琴


1
投票

您走在正确的轨道上,但主要问题是您不断地覆盖

search
值,而不是在每次迭代中存储它。

function blue() {
  var search;
  var index;
  var items = [];
  var inStock = [
    'apples',
    'eggs',
    'milk',
    'cookies',
    'cheese',
    'bread',
    'ham',
    'carrot',
    'broccoli',
    'pizza',
    'crackers',
    'onion',
    'banana',
    'frozen dinner',
    'cereals'
  ];

  alert("You start walking on the blue road");
  alert("You spot a grocery store at the side of the road.");
  alert("You decide to walk inside, and buy yourself some food.");
  alert("You open the door to the store, you hear a loud bell as you opened the door.");
  alert("You notice the cashier turning her head towards you.");
  alert("You take a bag, and nods politely, then walks in to the store.");
  alert("Heres a list of all the things available in the store.");

  search = promptMessage(inStock);

  while (search !== 'done') {
    index = inStock.indexOf(search);

    if (index > -1) {
      items.push(inStock.splice(index, 1)[0]);
      alert("You grabbed a " + search + " and put it in the bag");
    } else {
      alert("Sorry, the store is out of " + search);
    }
    search = prompt(inStock);
  }

  grocery(items);
}

function promptMessage(inStock) {
  return prompt(inStock + " Choose wich things you wanna buy, type 'done' when you are done. Type one thing at the time.");
}

function grocery(items) {
  alert("You decided that its enough");
  alert("In your bag you have " + items);
}

blue();


1
投票

您应该将商品存储在新数组中,如果商品有效,则从 inStock 数组中删除商品。

概念证明(也添加了缺失的分号):

function blue() {
	alert("You start walking on the blue road.");
	alert("You spot a grocery store at the side of the road.");
	alert("You decide to walk inside, and buy yourself some food.");
	alert("You open the door to the store, you hear a loud bell as you opened the door.");
	alert("You notice the cashier turning her head towards you.");
	alert("You take a bag, and nods politely, then walks in to the store.");
	alert("Heres a list of all the things available in the store.");

	var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'ham', 'carrot', 'broccoli', 'pizza', 'crackers', 'onion', 'banana', 'frozen dinner', 'cereals' ];
	var results = [];
	var search;

	while (search !== 'done') {
		search = prompt(inStock.join(', ') + "\n" + "Choose which things you wanna buy, type 'done' when you are done." + "\n" + "Type one thing at the time.");
		
		if (search === "done") {
			grocery();
			break;
		} else if (inStock.indexOf(search) > -1) {
			alert("You grabbed a " + search + " and put it in the bag");
			results.push(search);
			inStock.splice(inStock.indexOf(search), 1);
		} else {
			alert("Invalid item.");
		}
	}

	function grocery() {
		alert("You decided that its enough")
		alert("In your bag you have " + results.join(', '));
	}
}

blue();

© www.soinside.com 2019 - 2024. All rights reserved.