-1

如何在同一行输出?目前,它在垂直列表中列出;

但我想像这样“lAbI...”

代码有效。这只是输出结果的问题。请提供简单的解决方案。

   var words ="kzahdjdnshdkjts";
   var letters = words.split("");

   var final = "";
   for(let letter of letters){

    if(letter === "z"){
        letter = "`";
    }

  final = letter.charCodeAt();
  final += 1;

    if(final === 97){
            final = 65;
       }else if(final === 101){
            final = 69;
       }else if(final === 105){
            final = 73;
       }else if(final === 111){
            final = 79;
       }else if(final === 117){
            final = 85;
                } 

      final = String.fromCharCode(final);
      console.log(final);
       }
4

5 回答 5

2

创建第二个字符串(在本例finalString中为 ),该字符串最初为空,并附加到它而不是覆盖它(用+=)。

您还需要确保在循环console.log()外部运行:for

var words = "kzahdjdnshdkjts";
var letters = words.split("");

var final = "";
var finalString = "";
for (let letter of letters) {

  if (letter === "z") {
    letter = "`";
  }

  final = letter.charCodeAt();
  final += 1;

  if (final === 97) {
    final = 65;
  } else if (final === 101) {
    final = 69;
  } else if (final === 105) {
    final = 73;
  } else if (final === 111) {
    final = 79;
  } else if (final === 117) {
    final = 85;
  }

  finalString += String.fromCharCode(final);
}

console.log(finalString);

希望这可以帮助!:)

于 2017-09-07T20:47:08.500 回答
1

您可以通过将输出添加到节点来将输出作为 HTML 文本。

var words = "kzahdjdnshdkjts";
var letters = words.split("");

var final = "";
for (let letter of letters) {

  if (letter === "z") {
    letter = "`";
  }

  final = letter.charCodeAt();
  final += 1;

  if (final === 97) {
    final = 65;
  } else if (final === 101) {
    final = 69;
  } else if (final === 105) {
    final = 73;
  } else if (final === 111) {
    final = 79;
  } else if (final === 117) {
    final = 85;
  }

  final = String.fromCharCode(final);
  document.body.innerHTML += final;
}

于 2017-09-07T20:44:09.580 回答
0

您可以先附加字母,然后将最后的字符串打印到控制台。

   var words ="kzahdjdnshdkjts";
   var letters = words.split("");

   var veryfinal = "";
   var final = "";
   for(let letter of letters){

    if(letter === "z"){
        letter = "`";
    }

  final = letter.charCodeAt();
  final += 1;

    if(final === 97){
            final = 65;
       }else if(final === 101){
            final = 69;
       }else if(final === 105){
            final = 73;
       }else if(final === 111){
            final = 79;
       }else if(final === 117){
            final = 85;
                } 

      final = String.fromCharCode(final);
      veryfinal=veryfinal + final;

       }
             console.log(veryfinal);

于 2017-09-07T20:48:29.073 回答
0

您可以.map()将拆分为新的字符代码数组的数组传递给String.fromCharCode().

var words = "kzahdjdnshdkjts";

console.log( 
  String.fromCharCode(...words.split("").map(c => {
    const final = (c === "z" ? "`" : c).charCodeAt() + 1;

    switch (final) {
    case 97:  return 65;
    case 101: return 69;
    case 105: return 73;
    case 111: return 79;
    case 117: return 85;
    default:  return final;
    }
  }))
)

所以现在你只有一次调用String.fromCharCode,它使用扩展语法获取所有替换的字符代码的数组。

这种方式更快更清晰。

于 2017-09-07T20:50:27.487 回答
0

我没有尝试过这段代码,但这可能有效:(用你的代码的最后 3 行替换它。)

var finalString = "";
final = String.fromCharCode(final);
    finalString.concat(final)
}
console.log(finalString);

您需要在循环内连接字符串并在循环外打印。

于 2017-09-07T20:52:38.390 回答