📜  反转字符串 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:16.639000             🧑  作者: Mango

代码示例6
// Reverse string

function reverseString(str) {
    let reverseStr = '';
    let i = str.length - 1;

    while (i >= 0) {
        reverseStr += str[i];
        console.log(reverseStr);
        i--;
    }
    return reverseStr;
}
reverseString('hello');

// OR

function reverseString(str) {
    for (var reversedStr = '', i = str.length - 1; i >= 0; i--) {
        reversedStr += str[i];
    }
    return reversedStr;
}

// OR

function reverseString(str( {
    result = ""
      for (i = 0; i < str.length; i++) {
          result = str[i] + result
      }
      return result 
}

// OR

function reverseString(str) {
    return str.split('').reverse().join('');
}