使用循环或条件循环旋转字符串程序
Rotate string program using either loops or conditionals
我目前处于学习 JS 的早期阶段,我正在尝试创建一个简单的程序,需要
字符串并旋转它。我的意思是它把激光字符放在前面,依此类推,直到你有一个完整的圆圈。
我看到了一些与我想到的完全不同的解决方案。我一直在尝试用一段时间来做这件事,如果是的话。所以 while(string==string.length) 做 X.
我有一段带有 pop 和 unshift 的代码。我想打印出来安慰整个圈子。
let string = Array.from('w3resource');
let removeLast = string.pop();
console.log(removeLast);
let insertFirst = string.unshift(removeLast);
console.log(string);
你的开始很好,你现在需要按照你说的遍历数组。
let string = Array.from('w3resource');
for (let i = 0; i < string.length; i++) {
let removeLast = string.pop();
let insertFirst = string.unshift(removeLast);
console.log(string);
}
VM355:6 (10) ["e", "w", "3", "r", "e", "s", "o", "u", "r", "c"]
VM355:6 (10) ["c", "e", "w", "3", "r", "e", "s", "o", "u", "r"]
VM355:6 (10) ["r", "c", "e", "w", "3", "r", "e", "s", "o", "u"]
VM355:6 (10) ["u", "r", "c", "e", "w", "3", "r", "e", "s", "o"]
VM355:6 (10) ["o", "u", "r", "c", "e", "w", "3", "r", "e", "s"]
VM355:6 (10) ["s", "o", "u", "r", "c", "e", "w", "3", "r", "e"]
VM355:6 (10) ["e", "s", "o", "u", "r", "c", "e", "w", "3", "r"]
VM355:6 (10) ["r", "e", "s", "o", "u", "r", "c", "e", "w", "3"]
VM355:6 (10) ["3", "r", "e", "s", "o", "u", "r", "c", "e", "w"]
编辑 while 循环:
let string = Array.from('w3resource');
let j = 0;
while (j < string.length) {
let removeLast = string.pop();
let insertFirst = string.unshift(removeLast);
console.log(string);
j++;
}
稍后编辑:
您也可以使用 Array 中的 .reverse()
非常直接地完成此操作。但这不会是增量的,所以你不能逐行记录。但是您可以使用 .map()
在功能上重新实现 reveres
Array.from('w3resource').reverse()
(10) ["e", "c", "r", "u", "o", "s", "e", "r", "3", "w"]
当您使用从 w3resource
字符串创建的数组时,您可以使用 const
而不是 let 来存储值,因为 arrays
操作是通过它们的引用完成的.
const wordToRotate="w3resource".split("")//With split, you can define a delimiter.
完整圆圈的问题是旋转会在最后给出完全相同的字符串
要获得更多 flexible
,我将建议您使用可以拥有 2 parameters
:
的功能
- 要旋转的数组
- 您要执行的旋转次数
如果您知道 functions
的基本工作原理,您可以为 left rotation
创建可重复使用的 functions.One 之王,您可以在 shift
处删除项目数组的最左边或开头 push
和或 right rotation
从 very right
或 end
中删除 items
的 [=] 27=] 与 pop() method
和 unshift method
的开头。
左旋转
function rotateLeft(arr, numberOfRotations) {
const arrayToRotate = [...arr];
for (let rotationCount = 1; rotationCount <= numberOfRotations; rotationCount++) {
const itemToRemoveAtTheLeft = arrayToRotate.shift();
arrayToRotate.push(itemToRemoveAtTheLeft);
}
return arrayToRotate;
}
右旋
function rotateRight(arr, numberOfRotations) {
const arrayToRotate = [...arr];
for (let rotationCount = 1; rotationCount <= numberOfRotations; rotationCount++) {
const itemToRemoveAtTheLeft = arrayToRotate.pop();
arrayToRotate.unshift(itemToRemoveAtTheLeft);
}
return arrayToRotate;
}
现在您可以调用您想要的function
:
const charArray="w3resource".split("")//Or const charArray=Array.from("w3resource")
rotateLeft(charArray,3)
或 rotateRight(charArray,3)
希望对你有帮助
我目前处于学习 JS 的早期阶段,我正在尝试创建一个简单的程序,需要 字符串并旋转它。我的意思是它把激光字符放在前面,依此类推,直到你有一个完整的圆圈。 我看到了一些与我想到的完全不同的解决方案。我一直在尝试用一段时间来做这件事,如果是的话。所以 while(string==string.length) 做 X.
我有一段带有 pop 和 unshift 的代码。我想打印出来安慰整个圈子。
let string = Array.from('w3resource');
let removeLast = string.pop();
console.log(removeLast);
let insertFirst = string.unshift(removeLast);
console.log(string);
你的开始很好,你现在需要按照你说的遍历数组。
let string = Array.from('w3resource');
for (let i = 0; i < string.length; i++) {
let removeLast = string.pop();
let insertFirst = string.unshift(removeLast);
console.log(string);
}
VM355:6 (10) ["e", "w", "3", "r", "e", "s", "o", "u", "r", "c"]
VM355:6 (10) ["c", "e", "w", "3", "r", "e", "s", "o", "u", "r"]
VM355:6 (10) ["r", "c", "e", "w", "3", "r", "e", "s", "o", "u"]
VM355:6 (10) ["u", "r", "c", "e", "w", "3", "r", "e", "s", "o"]
VM355:6 (10) ["o", "u", "r", "c", "e", "w", "3", "r", "e", "s"]
VM355:6 (10) ["s", "o", "u", "r", "c", "e", "w", "3", "r", "e"]
VM355:6 (10) ["e", "s", "o", "u", "r", "c", "e", "w", "3", "r"]
VM355:6 (10) ["r", "e", "s", "o", "u", "r", "c", "e", "w", "3"]
VM355:6 (10) ["3", "r", "e", "s", "o", "u", "r", "c", "e", "w"]
编辑 while 循环:
let string = Array.from('w3resource');
let j = 0;
while (j < string.length) {
let removeLast = string.pop();
let insertFirst = string.unshift(removeLast);
console.log(string);
j++;
}
稍后编辑:
您也可以使用 Array 中的 .reverse()
非常直接地完成此操作。但这不会是增量的,所以你不能逐行记录。但是您可以使用 .map()
Array.from('w3resource').reverse()
(10) ["e", "c", "r", "u", "o", "s", "e", "r", "3", "w"]
当您使用从 w3resource
字符串创建的数组时,您可以使用 const
而不是 let 来存储值,因为 arrays
操作是通过它们的引用完成的.
const wordToRotate="w3resource".split("")//With split, you can define a delimiter.
完整圆圈的问题是旋转会在最后给出完全相同的字符串
要获得更多 flexible
,我将建议您使用可以拥有 2 parameters
:
- 要旋转的数组
- 您要执行的旋转次数
如果您知道 functions
的基本工作原理,您可以为 left rotation
创建可重复使用的 functions.One 之王,您可以在 shift
处删除项目数组的最左边或开头 push
和或 right rotation
从 very right
或 end
中删除 items
的 [=] 27=] 与 pop() method
和 unshift method
的开头。
左旋转
function rotateLeft(arr, numberOfRotations) {
const arrayToRotate = [...arr];
for (let rotationCount = 1; rotationCount <= numberOfRotations; rotationCount++) {
const itemToRemoveAtTheLeft = arrayToRotate.shift();
arrayToRotate.push(itemToRemoveAtTheLeft);
}
return arrayToRotate;
}
右旋
function rotateRight(arr, numberOfRotations) {
const arrayToRotate = [...arr];
for (let rotationCount = 1; rotationCount <= numberOfRotations; rotationCount++) {
const itemToRemoveAtTheLeft = arrayToRotate.pop();
arrayToRotate.unshift(itemToRemoveAtTheLeft);
}
return arrayToRotate;
}
现在您可以调用您想要的function
:
const charArray="w3resource".split("")//Or const charArray=Array.from("w3resource")
rotateLeft(charArray,3)
或 rotateRight(charArray,3)
希望对你有帮助