-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.js
129 lines (92 loc) · 2.37 KB
/
arguments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//Purpose: Genrates with HTML inputs an empty matrix
function generateMatrix(rows,columns)
{
var button = document.getElementsByClassName('btnCALC');
var matrix = document.getElementById('matrix');
var input = document.getElementById('usr-input');
//Making buttons "FIXED" and "VARIADIC" visible to the user
button[0].style.visibility = "visible";
button[1].style.visibility = "visible";
//Making user input visible
input.style.visibility = "visible";
//Reset previous Matrix
matrix.innerHTML = "<h3>" + rows + " x " + columns + " Matrix</h3>";
/*
A basic way to code 2D elements by concertinaing two for loops:
C O L U M N
R x x x x x x
O x x x x x x
W x x x x x x
For better clarification:
ROW: x x x x x x
COLUMN: x
x
x
*/
for(var row = 0; row < rows; row++)
{
for(var column = 0; column < columns; column++)
{
/*
NOTE:
The attribute name="elements" is here very important, because
it provides an array which is later needed.
*/
matrix.innerHTML += '<input type="number" name="elements" style="width:30px;" value="0">';
}
matrix.innerHTML += '<br>';
}
}
//Purpose: Shows the calculated result in Browser
function Calculate(fixed, variadic)
{
var result = document.getElementById('result');
var usrInput = document.getElementById('usr-input').value;
//After the matrix is generated, its elements should be accessible
var matrixElements = document.getElementsByName('elements');
var StringElements = document.getElementById('usr-input');
if(fixed)
{
result.innerHTML = calcAverageOfMatrix(matrixElements);
}
else if(variadic)
{
(function()
{
//Indirect call of "eval"
(0,eval)(usrInput);
})()
}
}
/*
ATTENTION:
This is the fixed-arity function
*/
function calcAverageOfMatrix(matrix)
{
var sum = 0;
var quantity = matrix.length;
for(var element = 0; element < quantity; element++)
{
//Should be "undefined" if not "true" which skips to "else"
if(matrix[element].value)
{
//"+" sign converts the ".value" from string to number
sum += (+matrix[element].value);
}
else
{
//Needed for "eval"
sum += matrix[element];
}
}
return (sum/quantity);
}
/*
ATTENTION:
This Variadic function is delegated to "calcAverageOfMatrix" by implementing it as a small wrapper.
*/
function calcAverage()
{
return calcAverageOfMatrix(arguments);
}