2
s1
またはs2
のスタックを選択でき、push
とpop
のような操作を実行できるスタックの作業モデルを作成しようとしています。チェックボックスをオンにしてスタックのオプションを選択できる対話型スタック
<html>
<head>
<title>Stack With Constructor </title>
</head>
<body>
<div>Stack </div>
<div>
<p id="p1">
stack
</p>
<input type="checkbox" name="stack1" value="s1">s1
<br>
<input type="checkbox" name="stack2" value="s2">s2
<br>
<textarea id="tdisplay"></textarea>
</div>
<textarea id="tpush"></textarea>
<button onclick="doJob()">push</button>
<button onclick="doJob1()">pop</button>
<textarea id="tpop"></textarea>
<script>
function myfunction() {
if (document.getElementById("stack1") == true) {
var s1 = new MyStack();
}
if (document.getElementById("stack2") == true) {
var s2 = new MyStack();
}
}
</script>
<script>
function push(v) {
if (this.st === 0) {
console.log("Stack Overflow");
} else {
this.st = this.st - 1;
this.stk[this.st] = v;
document.getElementById("tdisplay").value = this.print();;
}
}
function pop() {
if (this.st === 10) {
console.log("Stack Underflow");
} else {
document.getElementById("tdisplay").innerHTML = "";
var temp = this.stk[this.st];
this.st = this.st + 1;
return temp;
}
}
function print() {
console.log("Printing Stack");
return this.stk.reduce((acc, cv, i, arr,) => {
console.log(cv);
return (i) ? acc + " " + "\n" + cv : cv;
}, '');
}
function MyStack() {
this.st = 10;
this.stk = new Array(10);
this.push = push;
this.pop = pop;
this.print = print;
}
function doJob() {
var x = document.getElementById("tpush").value;
s1.push(x);
document.getElementById("tpush").value = "";
}
function doJob1() {
var y = s1.pop();
document.getElementById("tpop").value = y;
document.getElementById("tdisplay").value = s1.print();
}
</script>
</body>
</html>
次のように私は問題を取得しています:
私は
pop
値の出力を得ることができません。つまり、popのボタンを押すと配列の残りの項目が表示されます。checkboxメソッドの使い方はわかりません。つまり、
s1
チェックボックスを選択すると、s1
スタックが作成され、s2
と同じ操作を実行できます。彼/彼女は彼らができるその後、s1
を選択した場合- ユーザー
s1
またはs2
スタック - を選択します。それは、次の手順で同じように動作することができますので、
結果は、対話型のスタックのための適切なブラウザ機能をする必要があります
s1
のすべての操作で動作します。- ユーザー
- ユーザーは
s2
でも切り替えることができ、すべての操作を実行できます。
ポップ機能とチェックボックスの機能に問題があります。これは、あなたが正しい方向に始めるのに役立つだろう
私はポップ機能のための解決策が出ているが、まだチェックボックスのチェックボックス –
のために行く方法を考え出すことはないだろう: '' 'のdocument.getElementById(「stack1」)。 checked == true'''私は '' myFunction() ''で実装しました。 – Programmerrrrr