mardi 5 décembre 2017

interactive stack using checkbox s1 and s2 and different functions

I am trying to make working model of Stack in which user can select the stack s1 or s2 and can perform operations like push and pop.

<html>
    <head>
        <title>Stack With Constructor </title>
    </head>
    <body>
        
        <div>
            
            
            <p id="p1">
                stack
            </p>
            <input type="checkbox" id="stack1" value="s1">s1
            <br>
            <input type="checkbox" id="stack2" value="s2">s2
            <br>
            <textarea id="tdisplay"></textarea>
            <textarea id="tpush"></textarea>
            <button onclick="doJob()">push</button>
            
            <button onclick="doJob1()">pop</button>
            <textarea id="tpop"></textarea>
        </div>
        
        <script>
             function myfunction() {
        if (document.getElementById("stack1").checked == true) {
            console.log('myFunction called!');
            s1 = new MyStack();
        }
        if (document.getElementById("stack2").checked == true) {
            s2 = new MyStack();
        }
    }
            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 {
                    var temp = this.stk[this.st];
                    this.st = this.st + 1;
                    document.getElementById("tdisplay").value=this.print();  
                    return temp;
                }
            }

            function print() {
                console.log("Printing Stack");
                var str = "";//empty string
                for (var i = this.st ; i < 10; i++) {
                    console.log(this.stk[i]);
                    str+=this.stk[i]+'\n';//concatenate the value and a new line
                }
                return str;
            };

            function MyStack() {
                this.st = 10;
                this.stk = new Array(10);
                this.push = push;
                this.pop = pop;
                this.print = print;
            };

            var s1 = new MyStack();

            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;
                              
            }
        </script>
    </body>

I don't know how to use checkbox method. It means when i select s1 checkbox it should create s1 stack and I can do operations on it and same as for s2.

The result should be proper browser functions for interactive stack so it can work like in following steps:

User select s1 or s2 stack If he/she selects s1 then they can work for all operations for s1. User can switch between s2 too and can perform all operations. I am having issues on the checkbox working.




Aucun commentaire:

Enregistrer un commentaire