gpt4 book ai didi

javascript - 拼接不从数组 javascript 中删除项目

转载 作者:行者123 更新时间:2023-11-27 23:57:29 25 4
gpt4 key购买 nike

我的代码中的 splice 遇到问题,我不知道为什么不起作用,我需要从数组中删除最小整数。

这是我的代码:

var players = [
"Jug 1",
"Jug 2",
"Jug 3",
"Jug 4"
];

var arrTotal = [72, 71, 70, 75];

function winners(arr) {
var fstPlace = [], sndPlace= [];
var min = Math.min.apply(null, arr);
console.log(min);
for (var i = 0; i < arr.length; i++) {
if (arr[i] == min) {
fstPlace.push(arr.indexOf(min, i));
}
}
if (fstPlace.length == 1) {
console.log("1st: " + fstPlace);
arr.splice(min, 1);
console.log(arr);
}
else {
console.log("Tie: " + fstPlace);
}
}

winners(arrTotal);

最佳答案

您必须提供要删除的项目的索引

var players = [
"Jug 1",
"Jug 2",
"Jug 3",
"Jug 4"
];

var arrTotal = [72, 71, 70, 75];

function winners(arr) {
var fstPlace = [],
sndPlace = [];
var min = Math.min.apply(null, arr);
snippet.log('min: ' + min);

for (var i = 0; i < arr.length; i++) {
if (arr[i] == min) {
fstPlace.push(arr.indexOf(min, i));
}
}
snippet.log('fstPlace: ' + fstPlace)
if (fstPlace.length == 1) {
snippet.log("1st: " + fstPlace);
arr.splice(fstPlace[0], 1);
snippet.log('result: ' + arr);
} else {
snippet.log("Tie: " + fstPlace);
}
}

winners(arrTotal);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

关于javascript - 拼接不从数组 javascript 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132075/

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