简体   繁体   中英

how to store function value in variable and display it on textarea

I am trying to store value of function in the variable and want to display the content in text area t1 .

So every time I push the value I can get the print function in the variable s and can display content in textarea t1 .

<html>
    <head>
        <title>Stack With Constructor </title>
    </head>
    <body>
        <div>Stack </div>
        <div>
            <h1>stack</h1>
            <h2>Stage1.</h2>
            <p id="p1">
                stack
            </p>
            <textarea id="t1"></textarea>
            <button onclick="doJob()">push</button>
            <button onclick="doJob1()">pop</button>
        </div>
        <textarea id="t"></textarea>
        <script>
            function push(v) {
                if (this.st === 0) {
                    console.log("Stack Overflow");
                } else {
                    this.st = this.st - 1;
                    this.stk[this.st] = v;
                }
            }

            function pop() {
                if (this.st === 10) {
                    console.log("Stack Underflow");
                } else {
                    var temp = this.stk[this.st];
                    this.st = this.st + 1;
                    return temp;
                }
            }

            function print() {
                console.log("Printing Stack");
                for (var i = this.st ; i < 10; i++) {
                    console.log(this.stk[i]);
                }
            };

            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("t").value;
                s1.push(x);
                var s=s1.print();
                document.getElementById("t1").value=s;
            }
        </script>
    </body>
</html>

I want to display the print function in the text area t1 so it can work as interactive stack.

When I try to push value, I am getting undefined in the textarea.

change your print function like,

        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;
        };

DEMO

 <html> <head> <title>Stack With Constructor </title> </head> <body> <div>Stack </div> <div> <h1>stack</h1> <h2>Stage1.</h2> <p id="p1"> stack </p> <textarea id="t1"></textarea> <button onclick="doJob()">push</button> <button onclick="doJob1()">pop</button> </div> <textarea id="t"></textarea> <script> function push(v) { if (this.st === 0) { console.log("Stack Overflow"); } else { this.st = this.st - 1; this.stk[this.st] = v; } } function pop() { if (this.st === 10) { console.log("Stack Underflow"); } else { var temp = this.stk[this.st]; this.st = this.st + 1; 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("t").value; s1.push(x); var s=s1.print(); document.getElementById("t1").value=s; } function doJob1(){ s1.pop(); var s=s1.print(); document.getElementById("t1").value=s; } </script> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM