«Str.fromCharCode не является функцией» - PullRequest
0 голосов
/ 30 апреля 2018

Я получаю следующие ошибки:

  1. str.fromCharCode не является функцией
  2. newStr.push не является функцией

Понятия не имею, почему я получаю эти ошибки. Возможно, я неправильно использую методы

function rot13(str) {
  var newStr = str;
  for (i = 0; i < str.length; i++) {
    str.fromCharCode(str[i] - 13);
    newStr.push(i);
  }
  return newStr;
}

// Change the inputs below to test
console.log(
  rot13("SERR PBQR PNZC")
)  

Ответы [ 2 ]

0 голосов
/ 30 апреля 2018

Это String.fromCharCode , а не myString.fromCharCode

Наконец, вы хотите, чтобы charCodeAt вычитали из

Также нельзя вставить символ в строку. push это метод Array

function rot13(str) {
  var newStr = []; // using an array - you can use += to concatenate to string
  for (i = 0; i < str.length; i++) {
    // I suggest you do not convert the space. 
    // Here I converted it to another type of space but you can use " " if you want
    var x = str[i] == " " ? "\u2005":String.fromCharCode(str[i].charCodeAt(0) - 13);
    newStr.push(x);
  }
  return newStr.join("");
}

// Change the inputs below to test
console.log(
  rot13("SERR PBQR PNZC")
)  
0 голосов
/ 30 апреля 2018

Вы можете попробовать что-то вроде:

function rot13(str) { 
  var newStr = [];
  for(i = 0; i < str.length; i++){
    let x = String.fromCharCode(str[i].charCodeAt()-13);
    newStr.push(x);
  }
  return newStr.join("");
}
...