I am receiving undefined when using charAt[i]

Posted on
  • I am not sure why I am receiving undefined when I loop through data which is a string, To see if data contained a string I did console.log(typeof data + ": " + data); which echoed out String : 26.19. From my understanding, I thought looping through the data string and using charAt[i] I should in theory return the character at i, however, I am receiving undefined. Why is that and how do I return the character at i?

    s7s.prototype.sendValue = function (data) {
      var a = this.address;
      var length = data.length;
      var i;
      var val;
      var dpLoc;
    
      console.log(typeof data + ": " + data); //echos out a string
    
      for(i=0; i<data.length; i++) {
        var char = data.charAt[i];
    
        console.log(data.charAt[i]);
    
        if(char != ".") {
          val += char;
        }
    
        if(char == ".") {
          dpLoc = i;
        }
      }
    
      //console.log(val);
    
      //I2C2.writeTo(a, value);
    };
    
  • Using var char = data.substr(i, 1); seems to work.

  • Finally found it! Try data.charAt(i) instead of data.charAt[i], or simply data[i] :)

  • doh! I used brackets instead of parentheses. data.charAt(i); works.

    Code:

    s7s.prototype.sendValue = function (data) {
      var a = this.address;
      var clr = this.displayTable.Clear; //Display clear command
      var dpc = this.displayTable.dp.cmd; //Decimal point command
      var dplo = this.displayTable.dp.location; //Decimal point location map
    
      var i;
      var length = data.length;
      var dpLoc;
      var value = "";
    
      for(i=0; i<length; i++) {
        var char = data.charAt(i);
    
        if(char != ".") {
          value += char;
        }
    
        if(char == ".") {
          dpLoc = i;
        }
      }
    
      I2C1.writeTo(a, clr); //Clear display
      I2C1.writeTo(a, value);
    
      if(typeof dpLoc !== 'undefined') {
        I2C1.writeTo(a, dpc);
        I2C1.writeTo(a, dplo[dpLoc - 1]);
      }
    };
    
  • Two thoughts:

    if (char != ".") { value += char; }
    else { dpLoc = i; }
    

    'data' always has a decimal point?

    I2C1.writeTo(a, dpc);
      if(typeof dpLoc !== 'undefined') {
        I2C1.writeTo(a, dplo[dpLoc - 1]);
      }
    
  • No, results don't always have to return in a decimal format, however, in this case they will. I have
    if(typeof dpLoc !== 'undefined')
    For the moments where I wont, for example, taking a PPM reading of a solution. That was my attempt to write a generic function to output a result to my 4 - digit 7 segment display.

  • Doh, I get it now, wasn't reading carefully enough and missed the two I2CI.writeTo()'s before the decimal point check, sorry, my bad!

  • If data is 1234 and there is no decimal point in the value, the display will only show 1234. I still have to add checks for how many digits and if the display comma needs to be enabled or not. For example, 25.2'C. I have an idea:
    code:

    if (char == "'") { 
    I2C1.writeTo (a,0b00001000);  //turn on comma}
    
  • yeah, sorry, I just realized that and edited my comment, sorry :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

I am receiving undefined when using charAt[i]

Posted by Avatar for d0773d @d0773d

Actions