-2

I have a function that returns a string:

function buildCell(){
     var returnString = '';
     returnString += '<td>';
     returnString += 'Cell Contents';
     returnString += '</td>';
     return(returnString);
}

I have another function that should invoke the first string and return its value inline:

function buildTable(){
    $('body').html(
         '<table>'
         +'<tr>'
         + buildCell()
         +'</tr>'
    )
}

My table doesn't have the cell, the cell comes back undefined. How do i properly concatenate the return of the first function in the second?

Edit: I removed the semicolon and still receive "undefined". When I place an alert in the first function I see the correct value in the alert, but it is still undefined in the second function.

user1955162
  • 797
  • 2
  • 6
  • 21
  • 1
    You have incorrectly placed semi-colons inside buildTable function > buildCell(); – elad.chen Jun 24 '16 at 19:50
  • I tried it without the semicolon, and still get undefined. – user1955162 Jun 24 '16 at 19:54
  • You mean a cell isn't added ? or you're expecting buildTable to return something other than undefined ? – elad.chen Jun 24 '16 at 19:56
  • Yes, the cell is not added, just the text "undefined" when I Inspect Element – user1955162 Jun 24 '16 at 19:56
  • Please have another look at my answer. I included a snippet which clearly works. – elad.chen Jun 24 '16 at 20:01
  • 1
    your code works just fine. problem is somewhere else. – Kevin B Jun 24 '16 at 20:04
  • Who is calling `buildTable` can you post that please. I dont get any undefined in this example http://jsbin.com/xagidonepo/edit?html,css,js,output which is pretty much what @elad.chen did – Yordis Prieto Lazo Jun 24 '16 at 20:04
  • 2
    Questions *seeking debugging help ("why isn't this code working?")* **must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself**. Questions without a clear problem statement are not useful to other readers. - Your question currently does not provide code that recreates the problem. – Kevin B Jun 24 '16 at 20:06
  • In your question, put the alert in your code snippet where it shows undefined. Talking about code you don't put in your question can never be as precise as just including it to the letter. – trincot Jun 24 '16 at 20:07

2 Answers2

1

You have incorrectly placed semi-colons inside buildTable function > buildCell();

Change your function from this:

function buildTable(){
    $('body').html(
         '<table>'
         +'<tr>'
         + buildCell(); // <---
         +'</tr>'
    )
}

To this:

function buildTable() {
    return $('body').html('<table>' + '<tr>' + buildCell() + '</tr>');
}

https://jsfiddle.net/k0ageq81/

function buildCell() {
  var returnString = '';
  returnString += '<td>';
  returnString += 'Cell Contents';
  returnString += '</td>';
  return returnString;
}

function buildTable() {
  return $('body').html('<table>' + '<tr>' + buildCell() + '</tr>');
}

buildTable();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
elad.chen
  • 2,375
  • 5
  • 25
  • 37
1

This works:

  <body>
    <script>
      function buildCell() {
        var returnString = '';
        returnString += '<td>';
        returnString += 'Cell Contents';
        returnString += '</td>';
        return(returnString);
      }
      function buildTable(){
        $('body').html(
          '<table>'
          +'<tr>'
          + buildCell()
          +'</tr>'
        );
      }
      buildTable();
    </script>
  </body>
Quasifredo
  • 41
  • 4