Let’s say I have the following code, and I want to have N1 and N2 equal the inputs of numberInput1 and numberInput2 respectively. How would I do that?
JavaScript
x
12
12
1
"use strict";
2
// Alternative formula: MN = (N1 * (N2 - 2)) + (N1 + (N2 - 1))
3
4
N1 = numberInput1;
5
N2 = numberInput2;
6
7
// side number
8
SN = N1 + (N2 - 1);
9
// inside number
10
IN = N1 * (N2 - 2);
11
// multiplied number
12
MN = IN + SN;
JavaScript
1
25
25
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<meta charset="utf-8">
6
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7
<title>Multiplication Table Additive Pairs</title>
8
<meta name="description" content="calculates the multiplication table's additive pairs">
9
<meta name="viewport" content="width=device-width, initial-scale=1">
10
<link rel="stylesheet" href="styles.css">
11
</head>
12
13
<body>
14
<form
15
action="/Kinda Useful Programs/Text-based/Math-based/Calculations/Calculating Multiplication Table Additive Pairs/index.html"
16
method="GET">
17
<label for="input number">Please enter a number:</label>
18
<input type="text" name="input number 1" id="numberInput1" required>
19
<input type="text" name="input number 2" id="numberInput2" required>
20
<button type="submit">Submit</button>
21
</form>
22
<script src="script.js" async defer></script>
23
</body>
24
25
</html>
Advertisement
Answer
This is one way you could do it. It won’t create global variables, but if you really need global variables, you can modify to taste.
JavaScript
1
32
32
1
// DOM nodes to assist proof of concept
2
const input1 = document.getElementById('numberInput1');
3
const input2 = document.getElementById('numberInput2');
4
const output1 = document.querySelector('output[for="numberInput1"]');
5
const output2 = document.querySelector('output[for="numberInput2"]');
6
const outputSN = document.querySelector('output#sn');
7
const outputIN = document.querySelector('output#in');
8
const outputMN = document.querySelector('output#mn');
9
10
// here is the meat of your logic
11
const recalculateDerivedValues = () => {
12
const N1 = input1.valueAsNumber;
13
const N2 = input2.valueAsNumber;
14
const IN = N1 * (N2 - 2);
15
const SN = N1 + (N2 - 1);
16
outputSN.innerText = SN;
17
outputIN.innerText = IN;
18
outputMN.innerText = IN + SN;
19
};
20
21
// attach event handlers so values are updated in real time
22
const watchThenUpdate = (watchedElement, updateElement) => {
23
watchedElement.addEventListener('input', ev => {
24
updateElement.innerText = ev.target.value;
25
recalculateDerivedValues();
26
});
27
};
28
29
30
// attach event handlers to respective elements
31
watchThenUpdate(input1, output1);
32
watchThenUpdate(input2, output2);
JavaScript
1
13
13
1
output {
2
font-size: x-large;
3
font-weight: bold;
4
display: block;
5
font-family: Courier New;
6
color: maroon;
7
}
8
9
output::before {
10
content: attr(id);
11
padding-right: 1em;
12
text-transform: uppercase;
13
}
JavaScript
1
12
12
1
<form action="#" id="f" name="f">
2
<label for="input number">Please enter two numbers:</label>
3
<input type="number" name="input number 1" id="numberInput1" required>
4
<input type="number" name="input number 2" id="numberInput2" required>
5
</form>
6
7
<output form="f" for="numberInput1" id="N1"></output>
8
<output form="f" for="numberInput2" id="N2"></output>
9
10
<output id="sn"></output>
11
<output id="in"></output>
12
<output id="mn"></output>