1

I am new to Javascript and am having problems with looping my function.

I am trying to loop through a table and convert the numbers in the second column to a rating in the third column. I have defined a function to convert the numerical rating into text ("Good", "Bad", ...) which I then want to use in my loop. For some reason, the conversion works for the first row but stops at the second.

I couldn't find a answer on here, could anyone help? Thanks

  function convert(number) {
      if (number == 1) {
          return convert = "Bad";
      } else if (number == 2) {
          return convert = "Satisfactory";
      } else if (number == 3) {
          return convert = "Moderate Good";
      } else if (number == 4) {
          return convert = "Good";
      } else {
          return convert = "Excellent";
         }
  }



      var table = document.getElementById("results");
      var rows = table.rows;
      var rowcount = rows.length;

      console.log(rowcount);

      var num = [];
      var txt = [];

      for(var i = 1; i < rowcount ; i++) {
        num[i] = parseInt(table.rows[i].cells.item(1).innerHTML);
        txt[i] = convert( num[i] );
        table.rows[i].cells.item(2).innerHTML = txt[i];
      }
  <table id="results">
   <thead>
      <tr>
        <th>Question number</th>
        <th>Assessment value</th>
        <th>Output text</th>
      </tr>
    </thead>
    <tr>
        <td>Q1</td>
        <td>4</td>
        <td></td>
    </tr>
    <tr>
       <td>Q2</td>
        <td>4</td>
        <td></td>
    </tr>
    <tr>
       <td>Q3</td>
        <td>3</td>
        <td></td>
    </tr>
    <tr>
       <td>Q4</td>
        <td>2</td>
        <td></td>
    </tr>
    <tr>
       <td>Q5</td>
        <td>5</td>
        <td></td>
    </tr>
  </table>

1
  • I think it'd be more efficient to use a switch statement too. Commented Nov 13, 2015 at 11:24

2 Answers 2

1

The return statement usage was incorrect. In your code while returning value, it also changing the convert function reference to string "Good", so it was stoping in 2nd time execution. Please check the below corrected version.

  function convert(number) {
      if (number == 1) {
           return "Bad";
      } else if (number == 2) {
          return "Satisfactory";
      } else if (number == 3) {
          return  "Moderate Good";
      } else if (number == 4) {
          return  "Good";
      } else {
          return "Excellent";
      }
  }



      var table = document.getElementById("results");
      var rows = table.rows;
      var rowcount = rows.length;

      console.log(rowcount);

      var num = [];
      var txt = [];

      for(var i = 1; i < rowcount ; i++) {
        num[i] = parseInt(table.rows[i].cells.item(1).innerHTML);
        txt[i] = convert( num[i] );
        table.rows[i].cells.item(2).innerHTML = txt[i];
      }
  <table id="results">
   <thead>
      <tr>
        <th>Question number</th>
        <th>Assessment value</th>
        <th>Output text</th>
      </tr>
    </thead>
    <tr>
        <td>Q1</td>
        <td>4</td>
        <td></td>
    </tr>
    <tr>
       <td>Q2</td>
        <td>4</td>
        <td></td>
    </tr>
    <tr>
       <td>Q3</td>
        <td>3</td>
        <td></td>
    </tr>
    <tr>
       <td>Q4</td>
        <td>2</td>
        <td></td>
    </tr>
    <tr>
       <td>Q5</td>
        <td>5</td>
        <td></td>
    </tr>
  </table>

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

1 Comment

Thanks a lot, great explanation.
0

It's your convert function assigning return values to a variable rather than just returning them, and the fact that variable has the same name as the function.

When it does return convert = "whatever" it destroys itself, as the function itself is really just a variable.

Just use return "whatever" instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.