我是Javascript的新手。目前正在接受Javascript的任务,我必须在队列上工作。这是任务:
编写一个函数 nextInLine 它接受一个数组 (arr( 和一个数字 (项(作为参数。将数字添加到数组的末尾,然后 删除数组的第一个元素。下一个内联函数应该 返回已删除的元素。
function nextInLine(arr, item) {
// Your code here
return item; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
结果应该是这样的:
-
nextInLine([], 1)
应返回 1 -
nextInLine([2], 1)
应返回 2 -
nextInLine([5,6,7,8,9], 1)
应返回 5 nextInLine(testArr, 10)
后,testArr[4]
应为 10
你应该试试这个:
function nextInLine(arr, item) {
// Your code here
arr.push(item);
var returnable_value = arr[0];
arr.shift();
return returnable_value; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 4));
演示
function nextInLine(arr, item) {
arr.push(item);
return arr.shift();
}
console.log(nextInLine([], 1)); // 1
console.log(nextInLine([2], 1)); // 2
console.log(nextInLine([5,6,7,8,9], 1)); // 5
function nextInLine(arr, item) {
// Your code here
arr.push(item);
return arr.shift(); // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 4));