Okay. This is my first try of this Code Review invitation. Looking forward to learning ways to improve the code.
I would like to input real numbers from a textarea box and place them into a two-dimensional array.
The first number is an integer indicating the size of the SQUARE matrix.
The following numbers are all real numbers.
Scientific notation is NOT recognized and the user is informed of this limitation.
Here is the link to the working page: Eigenvalues and Eigenvectors Calculator
Here is the link directly to the external .js file that presently handles this task: http://www.akiti.ca/insysArealt.js
It is not pretty.
And here it is embedded in this post, if you don’t want to leave the forum:
<!--Define JavaScript functions.-->
function tinareal(rawtextareaString, inPar, sqAMatrix){
// Sub-routine for getting [A] matrix data from a textareabox:
// N, the size of the N x N matrix, and the
// A components.
// Assumes input string comes from a textarea box and that all entries represent real numbers.
var MAXDIM = 12; // Maximum size, N, of N x N matrix accepted by this script.
var rawArray = new Array(); // Array for holding raw data entry from textarea box
var rawArrayLength = 0; // The length of the raw array entered from textarea box
var numCoeff = 0; //Indicates the number of data entries that have been entered
var k = 0; // Array index
var tempx = 0.0; // Dummy variable
if (rawtextareaString.length == 0){
alert("The length of textareaString is 0. No data has been entered. No further action taken.");
inPar.inEr = 1;
return;
}
// At this point, rawtextareaString is a big long string containing the entire field entered in the textarea box.
// Must now clean up the data: remove leading and trailing spaces, identify the inputs, etc.
// Check to see that string contains digits; if not, no point continuing
if (rawtextareaString.search(/\\d/) == -1){
alert("textareaString does not contain any digits. No further action taken.");
inPar.inEr = 1;
return;
}
// Check to see that string contains only digits, decimal points, "+" or "-" sign, or white space; if not, no point continuing
if (rawtextareaString.search(/[^\\d\\.\\s\\+-]/) != -1){
alert("textareaString contains an invalid character. Please edit data so that it is in the appropriate format. No further action taken.");
inPar.inEr = 1;
return;
}
// Check to see that string contains newline characters;
// if not, then there are not AT LEAST two entries, and the algorithm won't work--no point continuing
if (rawtextareaString.search(/\
/) == -1){
alert("This utility requires at least two entries to be entered, but two entries have not been detected. Either fewer than two entries have been entered, or the data is not in the appropriate format. No further action taken.");
inPar.inEr = 1;
return;
}
// Do some rough clean up
rawtextareaString = rawtextareaString.replace(/\\s*$/, ''); // Remove trailing whitespace
rawtextareaString = rawtextareaString.replace(/^\\s*/, ''); // Remove leading whitespace
// Divide the string up into its individual entries, which--presumably--are separated by whitespace
rawArray = rawtextareaString.split(/\\s+/g);
rawArrayLength = rawArray.length;
// Check to see if at least two entries are present
if (rawArrayLength < 2){
alert("This utility requires AT LEAST two entries to be entered, but fewer than two entries have been detected. No further action taken.");
inPar.inEr = 1;
return;
}
// A maximum of 145 entries may be entered (first entry for the matrix dimension, up to 144 for the a-coefficients).
if (rawArrayLength > 145){
alert("This utility accepts input of up to 145 entries; however, more than 145 entries have been input. No further action taken.");
inPar.inEr = 1;
return;
}
numCoeff = rawArrayLength - 1;
// Now check the individual data entries, confirm they are valid numbers, and place the entries in the array
k = parseInt(rawArray[0]);
if (!isNaN(k)){ // Degree field contains a valid number; otherwise ignore
inPar.mDim = k;
}//End if !isNaN
else {
alert("Invalid input for matrix dimension. No further action taken.");
inPar.inEr = 1;
return;
} // End else
if (k > MAXDIM){
alert("This utility accepts matrices of dimension " + MAXDIM + " or less. However, a higher number has been input. No further action taken.");
inPar.inEr = 1;
return;
}
if (k <= 0 ){
alert("Negative values for the matrix dimension are not accepted. No further action taken.");
inPar.inEr = 1;
return;
}
if (numCoeff != (k*k)){
alert("The number of coefficients entered does not correspond to the matrix dimension entered. Check the data. No further action taken.");
inPar.inEr = 1;
return;
}
// Input the elements of the A matrix: A[0][0], A[0][1], A[0][2], . . . A[1][0], A[1][1], A[1][2], . . . etc.
k = 1;
for (var i = 0; i < inPar.mDim; i++) { // Examine the data fields
for (var j = 0; j < inPar.mDim; j++) {
tempx = parseFloat(rawArray[k]);
if (!isNaN(tempx)){ // Field contains a valid number
sqAMatrix[i][j] = tempx;
}//End if !isNaN
else {
alert("Invalid input for entry " + k + ". No further action taken.");
inPar.inEr = 1;
return;
} // End else
k++;
} // End for j
} // End for i
// *****************************************************************
// At this point, inPar.mDim should be the matrix dimension, N,
// and sqAMatrix should be the square matrix A, of size N x N.
// *****************************************************************
return;
} //End of tinareal
// end of JavaScript-->
It breaks down every check of possible errors and data clean-up into a separate if condition.
For example, check if any entries, check if enough entries, check if too many entries, check if numbers present, check if non-numbers present (other than a decimal point), etc.
If anything wrong is detected in the data, the error flag “inEr” in the input parameter is set to 1 and the program returns from this routine, so the main program can indicate an error and quit.
It would be nice to be able to combine or eliminate any of these checks. I’d like the code to be as efficient as possible; I’d like to use the same .js file to handle 2D matirx input of MANY other matrix manipulation routines.