在 JavaScript 中使用 for 循环反转字符串
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/2/22 3:24:17
我们需要编写一个 JavaScript 函数,该函数将字符串作为唯一参数。该函数应使用 for 循环根据输入字符串构造一个新的反转字符串。
示例
以下是代码 −
const str = 'this is the original string';
const reverseString = (str = '') => {
let reverse = '';
const { length: len } = str;
for(let i = len - 1; i >= 0; i--){
reverse += str[i];
};
return reverse;
};
console.log(reverseString(str));
输出
以下是控制台上的输出 −
gnirts lanigiro eht si siht
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

