首页 > 解决方案 > 破译任何传递的字符串

问题描述

我正在 Codewars 上尝试这个问题。我不确定这样做是否正确。这是问题:

您会收到一条需要破译的秘密信息。以下是你需要知道的东西来破译它:

对于每个单词:

the second and the last letter is switched (e.g. Hello becomes Holle)
the first letter is replaced by its character code (e.g. H becomes 72)

注意:没有使用特殊字符,只有字母和空格:

decipherThis('72olle 103doo 100ya'); // 'Hello good day'
decipherThis('82yade 115te 103o'); // 'Ready set go'

现在我写了这段代码:

function decipherThis(str)
{
  var msg = [];
  msg.push(str.charCodeAt(0));
  for (var i = 0; i<str.length; i++) 
  {
    if (str[1] == true && str[1] != str[str.length]) 
    {
      msg.push(str[str.length]);
      //str[1] = str[str.length]);
      var news = str;
      for (var j = 0; j<news.length; j++) 
      {
        news[1] = news[news.length];
        const newNew = delete news[0][1];

        msg.push(newNew);
      }
    }
  }
  return msg;
}; 

var google = "hello"

decipherThis(google)


我收到一个错误,我想我已经为一个单词创建了它。它不理解空格后的单词。请帮我解决这个问题。这是错误回溯:

  Response received but no data was written to STDOUT or STDERR. 

标签: javascriptarraysnode.jsencryption

解决方案


请更改characterCodeAtcharCodeAt,它将起作用。

characterCodeAt不是 String 类的有效方法


推荐阅读