javascriptfromcharcode

String.fromCharCode gives no result javaScript


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");


Solution

  • 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);