gpt4 book ai didi

javascript - .将整数插入数组在 JS/JQuery 中不起作用

转载 作者:行者123 更新时间:2023-11-28 06:44:10 25 4
gpt4 key购买 nike

这是我到目前为止的代码:

   var purseValue = 10000; 
var storePerson = [];
var storeAmount = [];

$('#prompt').keypress(function() { //
var whatMyUserTyped = $('#prompt').val(); //Input variable defined for future use.
if (event.keyCode == 13) { //If enter is pressed,
if (whatMyUserTyped === 'purse'){ //If used types "purse"
$('#log li:last').text('ShylockBot pulls out his purse containing '+ purseValue+ ' ducat(s)');
} else { //log the phrase with total amount in purse.
var subtractValue = parseInt(whatMyUserTyped.match(/\d+/)); //scan for the number in my string, then turn into a number.
var arrayOfInput = whatMyUserTyped.split(" ");
purseValue = (purseValue - subtractValue); //purse money minus the above sum equals current amount in purse.
storePerson.push(arrayOfInput[1]);
storeAmount.push(); //I tried to use the "subtractValue" but for some reason didn't work. So I just selected from string again.
console.log(storePerson);
console.log(subtractValue);

我试图获取用户输入,我从他们在字符串中输入的数字中获取,然后我还得到他们的名字(考虑到短语是:“贷款人 50 美元”)

所以 person 将是 arrayOfInput[1],而 number 将是他们输入的任何数字。

现在我尝试将这些值存储到 2 个数组中。

我使用了 .push() ,它对于“storePerson”效果很好,每次我的函数运行时,它都会将输入人员添加到其中,但我无法将其添加到数组中。它仅替换数组中的一个数字,并且不存储旧值,这意味着将 storeAmount 替换为最新输入的值。

尽管我是初学者,但我还是尽力说得清楚。所以我希望有人能帮助我。谢谢。

最佳答案

我读了很多遍,看起来你需要:

storeAmount.push(subtractValue[0]);
//感谢 Orleaner 指出 match 返回一个数组

你使用双引号吗?

PS:即使如此,它也不应该成为问题,它只会每次添加字符串“subrtractValue”:)

关于javascript - .将整数插入数组在 JS/JQuery 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33536120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com