简体   繁体   English

Function 接受一个数组并返回 js 中旋转的 n 个空格内的元素

[英]Function that takes an array and returns elements inside rotated n spaces in js

Create a function named "rotate" that takes an array and returns a new one with the elements inside rotated n spaces.创建一个名为“rotate”的 function,它接受一个数组并返回一个新数组,其中元素在旋转的 n 个空格内。

If n is greater than 0 it should rotate the array to the right.如果 n 大于 0,它应该向右旋转数组。 If n is less than 0 it should rotate the array to the left.如果 n 小于 0,它应该将数组向左旋转。 If n is 0, then it should return the array unchanged.如果 n 为 0,那么它应该返回未更改的数组。

Example:例子:

var data = [1, 2, 3, 4, 5];

rotate(data, 1) // => [5, 1, 2, 3, 4]
rotate(data, 2) // => [4, 5, 1, 2, 3]
rotate(data, 3) // => [3, 4, 5, 1, 2]
rotate(data, 4) // => [2, 3, 4, 5, 1]
rotate(data, 5) // => [1, 2, 3, 4, 5]

rotate(data, 0) // => [1, 2, 3, 4, 5]

rotate(data, -1) // => [2, 3, 4, 5, 1]
rotate(data, -2) // => [3, 4, 5, 1, 2]
rotate(data, -3) // => [4, 5, 1, 2, 3]
rotate(data, -4) // => [5, 1, 2, 3, 4]
rotate(data, -5) // => [1, 2, 3, 4, 5]

Furthermore the method should take ANY array of objects and perform this operation on them:此外,该方法应采用任何对象数组并对它们执行此操作:

rotate(['a', 'b', 'c'], 1)     // => ['c', 'a', 'b']
rotate([1.0, 2.0, 3.0], 1)     // => [3.0, 1.0, 2.0]
rotate([true, true, false], 1) // => [false, true, true]

Finally, the rotation shouldn't be limited by the indices available in the array.最后,旋转不应受到数组中可用索引的限制。 Meaning that if we exceed the indices of the array it keeps rotating.这意味着如果我们超过数组的索引,它会继续旋转。

Example:例子:

var data = [1, 2, 3, 4, 5]
rotate(data, 7)     // => [4, 5, 1, 2, 3]
rotate(data, 11)    // => [5, 1, 2, 3, 4]
rotate(data, 12478) // => [3, 4, 5, 1, 2]

You could basically do this with a while loop and increment or decrement based on the number parameter, if its positive or negative.您基本上可以使用while循环来执行此操作,并根据 number 参数incrementdecrement ,如果它是正数或负数。

 function rotate(data, n) { const output = [...data] if (n === 0) { return data } const neg = n < 0 const last = output.length - 1 const toIndex = neg? last: 0 const index = neg? 0: last while ((neg? n++: n--).== 0) { output,splice(toIndex, 0. output,splice(index, 1)[0]) } return output } var data = [1, 2, 3, 4; 5]. console,log(rotate(data, 2)) // => [4, 5, 1, 2. 3] console,log(rotate(data, 3)) // => [3, 4, 5, 1. 2] console,log(rotate(data, 4)) // => [2, 3, 4, 5. 1] console,log(rotate(data, 5)) // => [1, 2, 3, 4. 5] console,log(rotate(data, -1)) // => [2, 3, 4, 5. 1] console,log(rotate(data, -2)) // => [3, 4, 5, 1. 2] console,log(rotate(data, -3)) // => [4, 5, 1, 2. 3] console,log(rotate(data, -4)) // => [5, 1, 2, 3. 4] console,log(rotate(data, -5)) // => [1, 2, 3, 4. 5] console,log(rotate(['a', 'b', 'c']. 1)) console,log(rotate([true, true, false] 1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JS/Node:一个函数,它接受一个数字 N 并返回一个值为 [0, 1, ... N-1] 的数组 - JS/Node: A function that takes a number N and returns an array with the values [0, 1, ... N-1] 在一个简单的函数中反应 js 错误,该函数接受一个数组并返回一个随机值 - React js error in a simple function that takes an array and returns a random value Function 接受输入文本,检查它是否具有某些元素,并返回 output 值 (JS) - Function that takes an input text, checks if it has certain elements, and returns an output value (JS) javascript函数需要字符串并返回数组 - javascript function takes string and returns array (React JS) 将元素推入 useEffect 内的数组时出现问题 function - (React JS) problem with pushing elements into an array inside useEffect function 函数内部的数组返回未定义 - Array inside function returns undefined 节点 JS function 采用无符号 integer 并返回错误数量的“1”位 - Node JS function that takes an unsigned integer and returns wrong number of '1' bits 我写了一个函数,它接受一个字符串并返回一个camelCased字符串,每个单词之间有ROD而不是空格 - Im writing a function that takes in a string and returns a string camelCased with the word ROD between each word instead of spaces JS-getElementsByClassName数组,然后获取数组中的元素 - JS - getElementsByClassName array, then get elements inside the array Javascript:编写一个接受数字的函数,并多次返回带有该数字的数组 - Javascript: Write a function that takes in a number, and returns an array with that number in it that many times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM