Copy link to clipboard
Copied
Hello. In my aplication i save progress in txt files. Where i save all variables
....//example
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
....
My script load all fast and good, But problem is one. Its loades as String not Number
var variablesList = [];
var variableFile= new File("C://Users//FCT//Documents//testVar.txt"); //open order.txt file
variableFile.open('r');
while (!variableFile.eof) { //get all lines from order txt file
st = variableFile.readln();
variablesList.push(st);
} //end of all lines from order txt file
variableFile.close();
alert(startFrom);
apronBlackCounter=variablesList[0].split(',');
apronFuchsiaCounter=variablesList[1].split(',');
apronRedCounter=variablesList[2].split(',');
apronRoyalBlueCounter=variablesList[3].split(',');
How can i conver that to number when i cant use .map(Number);???
I shuld make" for (var i=0;apronBlackCounter.length; i++){ apronBlackCounter = parseInt(apronBlackCounter);} ". But it wont work...
Copy link to clipboard
Copied
what about this:
var myArray = myString.split(",");
This will convert string to array. Replace "," if you use other character as a separator.
Copy link to clipboard
Copied
This may help.
var variablesList = [];
var variableFile= new File("C://Users//FCT//Documents//testVar.txt"); //open order.txt file
variableFile.open('r');
while (!variableFile.eof) { //get all lines from order txt file
st = variableFile.readln();
variablesList.push(st);
} //end of all lines from order txt file
variableFile.close();
// alert(startFrom);
apronBlackCounter=variablesList[0].split(',');
apronFuchsiaCounter=variablesList[1].split(',');
apronRedCounter=variablesList[2].split(',');
apronRoyalBlueCounter=variablesList[3].split(',');
alert(typeof(apronBlackCounter[0]));
alert(typeof(Number(apronBlackCounter[0])));
Copy link to clipboard
Copied
You can use several things to convert a string to a number. For what you're doing, I use:
var myNum = parseInt('0')//will give an interger. This will strip any string elements like px after the number.
var myNum = parseFloat('0')//will give an floating number
var myNum = Number('0')//will not give just an integer if the original number is a floating point number. Will give NaN error if any string elements are included.
var myNum = Math.round('0')//will give an interger and round the number.
var myNum = Math.ceil('0')//will give an interger and always round up.
var myNum = Math.floor('0')//will give an interger and always round down.
Copy link to clipboard
Copied
Here is polyfill for map: Array.prototype.map() - JavaScript | MDN
Copy link to clipboard
Copied
Neat. learn something new from you all the time.