Reputation: 69
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?
Upvotes: 0
Views: 77
Reputation: 571
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);
Upvotes: 2