gpt4 book ai didi

javascript - 如何使用 javascript 在数组中查找顺序更改的内容?

转载 作者:行者123 更新时间:2023-12-01 15:06:15 25 4
gpt4 key购买 nike

    For Ex : original array : a =[1,2,3,4,g,5] 
if the user wrongly entered the above array like this : a = [2,1,3,4,g,5]

如何使用 javascript 从原始数组列表中找到位置更改的元素?(即 [1,2] 已更改)。

最佳答案

// Try entering this at the prompt: [2, 1, 3, 4, 'g', 5]

var original = [1, 2, 3, 4, 'g', 5],
input = eval(prompt('Enter an array please.'));

input.forEach(function (element, index) {
var expected = original[index];
if (element !== expected) {
// They are different.
// Logic to handle the difference goes here. Example:
console.log('Elements were different; ' +
'expected `' + expected + '\', ' +
'got `' + element + '\'.');
}
});

关于javascript - 如何使用 javascript 在数组中查找顺序更改的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245509/

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