0

I'm a begginer and I'm coding this to change values of a whole array. For instance, my aim is to change ºC to ºK. But this code doesn't seems to work. It gives an error message: "j is undefined". What's wrong with this code?

//Original Arrays

var I_t1=new Array();
var V_t1=new Array();

//Arrays de la tabla 1
var K_t1=new Array();
var P_t1=new Array();

function kelvin() {
    var i;

    var j = new Array();
    var k = new Array();

    var k;
    var j=V_t1.lenght;   

    var k=I_t1.lenght; // La k será igual a la longitud del array T

    for(i=0;i<j.length;i++){   
        K_t1[i]= (V_t1[i] * 200);
    }

    for(i=0;i<k.length;i++){      
        P_t1[i]= (I_t1[i] * 400);
    }
}

Until here, the code. So my question is:

-How do I modify this funcion to archieve its aim?

1
  • Several things: Think about indentation - it's almost impossible to read without it. Secondly, you're redeclaring k in the kelvin function a total of 3 times and j two times - why? Thirdly, I_t1.lenght should be I_t1.length. Fourthly, you're setting j and k to a NUMBER, yet you're trying to call .length on them and a number doesn't have a length. Skip out the .length part in the for statements. Commented Jun 12, 2013 at 8:25

2 Answers 2

3

Look at your spelling - it's .length, not .lenght!

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

2 Comments

Wow!! length... No more errors in the web browser console. Thanks a lot. EDIT: But still, it doesn't make any change in the values...
I am really happy that helped you! If something is undefined always look for spelling first - you may have missed a letter or swapped some :)
2

As others mentioned there is a problem in the .length. But I would recommend a different approach to solve this problem. Try and use higher order functions like "map" to make this work. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

e.g. A function to change ºC to ºK would be something like:

function celciusToKelvin(celciusVal){
   return celciusVal + 274.15;
}

Now if you have an array of celcius values like var celciusValues = [12,23,34,45];, you can call the map function like:

var kelvinValues = celciusValues.map(celciusToKelvin);

2 Comments

Hum, thanks! I'm going to try this solution changing the approach I've been following since now.
Wait a moment, there is something odd here. I will answer my own question to show the results.

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.