So I’m trying to print all values of x,y,z from this equation x − 2y + 3z = 0 (value of between 1-5, the value of y is between 6-10, and z is between 3-7) on HTML through javascript but the
JavaScript
x
2
1
document.getElementById("display").innerHTML = x,y,z;
2
Only displays the maximum value of x, y, z. instead of any possible solution. So I’m unsure whether there is a problem with the for loops and the way I’m trying to display on the HTML page. This is the output I want
JavaScript
1
47
47
1
function Solve() {
2
{
3
var x = [];
4
var y = [];
5
var z = [];
6
var ans = [];
7
// x-2y+3z=0
8
// x3[1,5] y3[6,10] z3[3,7]
9
console.log("xtytznn");
10
for (x = 1; x <= 5; x++) {
11
for (y = 6; y <= 10; y++) {
12
z = (2 * y - x) / 3.0;
13
if (x >= 0 && y >= 0 && z >= 0) {
14
if (x % 1 == 0 && y % 1 == 0 && z % 1 == 0) {
15
if (x >= 1 && x <= 5 && y >= 6 && y <= 10 && z >= 3 && z <= 7) {
16
(document.getElementById("display").innerHTML = x), y, z;
17
}
18
}
19
}
20
}
21
}
22
for (y = 6; y <= 10; y++) {
23
for (z = 3; z <= 7; z++) {
24
x = 2 * y - 3 * z;
25
if (x >= 0 && y >= 0 && z >= 0) {
26
if (x % 1 == 0 && y % 1 == 0 && z % 1 == 0) {
27
if (x >= 1 && x <= 5 && y >= 6 && y <= 10 && z >= 3 && z <= 7) {
28
document.getElementById("display").innerHTML = y;
29
}
30
}
31
}
32
}
33
}
34
for (z = 3; z <= 7; z++) {
35
for (x = 1; x <= 5; x++) {
36
y = (x + 3 * z) / 3.0;
37
if (x >= 0 && y >= 0 && z >= 0) {
38
if (x % 1 == 0 && y % 1 == 0 && z % 1 == 0) {
39
if (x >= 1 && x <= 5 && y >= 6 && y <= 10 && z >= 3 && z <= 7) {
40
document.getElementById("display").innerHTML = z;
41
}
42
}
43
}
44
}
45
}
46
}
47
}
JavaScript
1
16
16
1
<center>
2
<p>x - 2y + 3z = 0</p>
3
<button onclick="Solve()">Solve</button><br />
4
5
<span id="display"></span>
6
7
<span id="display"></span>
8
<span id="display"></span>
9
<span id="display"></span>
10
<span id="display"></span>
11
<span id="display"></span>
12
<span id="display"></span>
13
<span id="display"></span>
14
<span id="display"></span>
15
<span id="display"></span>
16
</center>
Advertisement
Answer
You can try following code to print the solutions for the equation:
html:
JavaScript
1
6
1
<center>
2
<p>x - 2y + 3z = 0</p>
3
<button onclick="Solve()">Solve</button><br />
4
<div id="solution" style="color: green"></div>
5
</center>
6
javascript :
JavaScript
1
18
18
1
function Solve()
2
{
3
const solutions=[];
4
for(let x=1;x<=5;x++)
5
for(let y=6;y<=10;y++)
6
for(let z=3;z<=7;z++)
7
if (x-2*y+3*z==0)
8
solutions.push([x,y,z]);
9
10
var solutionElement = document.getElementById("solution");
11
solutionElement.innerText = "The solutions are:"
12
solutions.forEach(solution => {
13
const node = document.createElement("div");
14
node.innerText = `x= ${solution[0]}, y=${solution[1]}, z=${solution[2]}`;
15
solutionElement.appendChild(node);
16
});
17
}
18
PS: You can apply style however you want.