0

Why code returns output + undefined?

A function, where argument h is equal 680131659347. Output is leepadgdenifednu. It's leepadg + denifednu (undefined).

var f = function (h) {
  var letters, result, i;

  i = 7;
  letters = "acdegilmnoprstuw";

  while (i) {
    result += letters[parseInt(h % 37)];
    h = h / 37;
    i--;
  }
  return result.split("").reverse().join("");
};
6
  • 4
    I get leepadgdenifednu, so it works fine, I suppose Commented Dec 18, 2014 at 4:11
  • Where exactly are you getting undefined? Please post a complete example Commented Dec 18, 2014 at 4:12
  • 1
    @AmitJoki leepadgdenifednu it's leepadg + denifednu (undefined) Commented Dec 18, 2014 at 4:12
  • 1
    Give result an initial value to concatenate onto -- var letters, result = '', i;. Commented Dec 18, 2014 at 4:12
  • What is the while condition? Commented Dec 18, 2014 at 4:12

4 Answers 4

2

You need to initialize the value of result, when you way var result, result will have a value of undefined then when you use it in a string concatenation the value will be undefined+<concatenated value>

var log = (function() {
  var $log = $('#log');
  return function(msg) {
    $('<p/>', {
      text: msg
    }).prependTo($log)
  }
})();

var f = function(h) {
  var letters, result = '',
    i;

  i = 7;
  letters = "acdegilmnoprstuw";

  while (i) {
    result += letters[parseInt(h % 37)];
    h = h / 37;
    i--;
  }
  return result.split("").reverse().join("");
};

log(f(680131659347))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="log"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

You were Not Initialize the Result and You are Try to Concate the value to result

 var f = function (h) {
      var letters, result, i;
    result=//Initialize Result
      i = 7;
      letters = "acdegilmnoprstuw";

      while (i) {
        result += letters[parseInt(h % 37)]; 
        h = h / 37;
        i--;
      }
      return result.split("").reverse().join("");
    };

Comments

0

use this:

 var f = function (h) {
      var letters, result, i;

      i = 7;
      letters = "acdegilmnoprstuw";
result="";// initailize result
      while (i) {
        result += letters[parseInt(h % 37)];
        h = h / 37;
        i--;
      }
      return result.split("").reverse().join("");
    };

Comments

0

result is not initialized as string so very first time of concatenation its generate an error like undefined then its ok , so initialize the result variable as result=''

var f = function (h) {
  var letters, result='', i;

  i = 7;
  letters = "acdegilmnoprstuw";

  while (i) {
    result += letters[parseInt(h % 37)];
    h = h / 37;
    i--;
  }
  return result.split("").reverse().join("");
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.