<!--
Additional.....
Question: What kind of structure and algorithm is needed to make a game like this?
An array of multiple stacks.
the index of the array could also match a visual column that
the list of numbers appears.
when implemented, clicking the operator performs the operation
on the items of the stack variable which is a member of the array
object.
2 3
3 5 2
9 1 1
arr[0] arr[1] arr[2]
stack stack stack
2.3.9 5.1 2.1.1
-+/*^ -+/*^ -+/*^
A " * " operation on column 0 and 2 result is both getting a 6 result.
-->
Enter Numbers or operators one at a time<br /><br />
0 1 2 3 4 5 6 7 8 9 + - * / ^<br /><br />
<p id="lastMessage"></p>
<input type="text" id="input" autofocus onkeydown="if (event.keyCode == 13) enterData()" />
<input type="button" value="enter" onclick="enterData();" />
<table width="100%">
<tr valign="top">
<td>
<p id="output">
</p>
</td>
<td width="50%">
<p id="stack">
</p>
</td>
</tr>
</table>
<!--
Module 4 - Assignment 1 - sheldon pasciak
Assignment 1 - Implement a Stack computer in Javascript (you will turn in a link to your program in JSFiddle). This is a simple computer that keeps a stack, when a number is entered it goes onto the top of the stack. When an operation is entered, the previous 2 numbers are operated on by the operation.
For example
2 [enter] 2
5 [enter] 5 2
* [enter] * 5 2 -> collapses to 10
would leave at 10 at the top of the stack.
The program should use a simple input box, either a text field or prompt and display the contents of the Stack.
-->
JavaScript
// Module 4 - Assignment 1 - sheldon pasciak - Implement a Stack computer in Javascript
// for use to display output to console and in window of fiddle
function printToScreen(msg) {
document.getElementById("output").innerHTML += msg + "<br />";
console.log(msg);
}
function Stack() {
this.top = null;
this.size = 0;
}
function Node(data) {
this.data = data;
this.previous = null;
}
//create new node making it the new top node
Stack.prototype.push = function(value) {
var node = new Node(value);
//document.getElementById("lastMessage").innerHTML = "push: " + value;
node.previous = this.top;
this.top = node;
this.size ++;
return this.top;
};
//return the value at the top node, removing top node in the process
Stack.prototype.pop = function() {
if (this.size>0) {
temp = this.top;
this.top = this.top.previous;
this.size -= 1;
//document.getElementById("lastMessage").innerHTML = "pop: " + temp.data;
return temp.data;
} else {
//The stack did not contain enough values or is EMPTY.
alert("The stack did not contain enough values or is EMPTY.");
return null;
}
};
//boolean to check if stack is empty
Stack.prototype.isEmpty = function() {
return (this.size<=0);
}
Stack.prototype.print = function() {
// reset window display of stack (displays stack top to bottom on right)
document.getElementById("stack").innerHTML = "Top Down View Of Stack<br />";
var current=this.top;
while (current) {
document.getElementById("stack").innerHTML += current.data + "<br />";
current = current.previous; // becomes null at top
}
}
function divide(a,b) {
document.getElementById("lastMessage").innerHTML = a + " / " + b ;
return (a/b);
}
function multiply(a,b) {
document.getElementById("lastMessage").innerHTML = a + " * " + b ;
return (a*b);
}
function subtract(a,b) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.