-1

after running code i get no result in window. and i cant find problem result have to be string created from charCode.

function rot13(str) {
  var te = [];
  var i = 0;
  var a = 0;
  var newte = [];

  while (i < str.length) {
    te[i] = str.charCodeAt(i);
    i++;
  }
  while (a != te.length) {
    if (te[a] < 65) {
      newte[a] = te[a] + 13;
    } else
      newte[a] = te[a];
    a++;
  }

  var mystring = String.fromCharCode(newte);


  return mystring;
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
EdenLT
  • 15
  • 6

2 Answers2

0

The method String.fromCharCode expects you to pass each number as an individual argument. In your code sample, you are passing an array as a single argument, which won't work.

Try using the apply() method instead, which will allow you to pass an array, and it will convert that into multiple individual arguments:

var mystring = String.fromCharCode.apply(null, newte);
Steven Schobert
  • 4,201
  • 3
  • 25
  • 34
0

Looks like String.fromCharCode() is not defined to operate on an array.

Try like this:

function rot13(str) {
  var result = "";
  
  for (var i = 0; i < str.length; i++) {
    var charCode = str.charCodeAt(i) + 1;
    
    if (charCode < 65) {
      charCode += 13;
    }
    
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

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

NOTE: I copied your logic for the character substitution, but it doesn't seem correct.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156