Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app" v-cloak>

  <b-container>
    <b-row>
      <b-col>
        <b-card title="Stocks">
          <b-list-group>
            <b-list-group-item v-for="stock in stocks" :key="stock.name">
              <b-container>
                <b-row>
                  <b-col>{{ stock.name }}</b-col>
                  <b-col class="text-right">{{ stock.price | money}}</b-col>
                </b-row>
              </b-container>
            </b-list-group-item>
          </b-list-group>
        </b-card>
      </b-col>
      <b-col>
        <b-card title="Holdings">
          <b-list-group>
            <b-list-group-item v-for="stock in stocks" :key="stock.name">
              <b-container>
                <b-row>
                  <b-col>{{ stock.name }}</b-col>
                  <b-col class="text-right">{{ stock.held}}</b-col>
                </b-row>
              </b-container>
            </b-list-group-item>
          </b-list-group>
          <p class="card-text">
            Total value of holdings: {{ holdingValue | money }}
          </p>
        </b-card>
      </b-col>
    </b-row>
    <!-- todo, add some vertical space -->
    <div style="height:20px"></div> <!-- this feels lame -->
    <b-card title="Purchase and Sell Stocks">

      <p class="card-text">
        Buy <b-form-input v-model.number="buy" type="number" style="width:100px;display:inline"></b-form-input> shares of 
        <b-form-select style="width:120px;display:inline" v-model="buyStock">
          <option v-for="stock in stocks" :key="stock.name">{{ stock.name }}</option>
        </b-form-select>
        <b-button :variant="'success'" @click="buyStocks" class="customBtn">Purchase</b-button>
        <span v-if="buyError">Not enough cash.</span>
      </p>
      <p class="card-text">
        Sell <b-form-input v-model.number="sell" type="number" style="width:100px;display:inline"></b-form-input> shares of 
        <b-form-select style="width:120px;display:inline" v-model="sellStock">
          <option v-for="stock in stocks" :key="stock.name">{{ stock.name }}</option>
        </b-form-select>
        <b-button :variant="'success'" @click="sellStocks" class="customBtn">Sell</b-button>
        <span v-if="sellError">Not enough on hand.</span>
      </p>

      <p class="card-text">
        You currently have {{ cash | money }} in cash.
      </p>

    </b-card>

  </b-container>


</div>
              
            
!

CSS

              
                #app {
	padding-top: 20px;
}

[v-cloak] { 
  display: none; 
}

.customBtn {
  width: 120px;
}
              
            
!

JS

              
                
//speed of how often stocks update
const STOCK_UPD = 2;

//Thank God for MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

const store = new Vuex.Store({
	state:{
		stocks:[
			{name:"IBM", price:100, held:0},
			{name:"Amazon", price:90, held:0},
			{name:"Microsoft", price:110, held:0},
			{name:"Disney", price:120, held:0},
		],
		holdings:[],
		cash:1000
	},
	getters:{
		holdingValue(state) {
			return state.stocks.reduce(function(current,elm) {
				return current + (elm.price * elm.held);
			},0); 
		}
	},
	mutations:{
		changeStocks(state) {
			state.stocks.forEach(stock => {
				stock.price += getRandomArbitrary(-20,20);
				if(stock.price < 0) stock.price = 0;
			});
		},
		buyStock(state, order) {
			console.log('order is '+JSON.stringify(order));
			//first, find the stock
			let stock = state.stocks.findIndex(elm => {
				return elm.name === order.stock;
			});
			if(stock >= 0) {
				let purchasePrice = (state.stocks[stock].price * order.amount).toFixed(2);
				if(state.cash >= purchasePrice) {
					state.stocks[stock].held += order.amount;
					state.cash -= purchasePrice;
				}
			}
		},
		sellStock(state, order) {
			console.log('order is '+JSON.stringify(order));
			//first, find the stock
			let stock = state.stocks.findIndex(elm => {
				return elm.name === order.stock;
			});
			if(stock >= 0) {
				if(state.stocks[stock].held >= order.amount) {
					let sellPrice = (state.stocks[stock].price * order.amount).toFixed(2);
					state.stocks[stock].held -= order.amount;
					state.cash += Number(sellPrice);
				}
			}
		}
	}
});

const app = new Vue({
	el:'#app',
	store,
	data() {
		return {
			buy:0,
			buyStock:null,
			buyError:false,
			sell:0,
			sellStock:null,
			sellError:false
		}
	},
	filters:{
		money(value) {
			let numb = Number(value).toFixed(2);
			return '$'+numb;
		}
	},
	mounted:function() {
		setInterval(() => {
			console.log('running stock update');
			store.commit('changeStocks');
		}, STOCK_UPD*1000);	
	},
	computed:{
		stocks() {
			return store.state.stocks;
		},
		holdingValue() {
			return store.getters.holdingValue;
		},
		cash() {
			return store.state.cash;
		}
	},
	methods:{
		buyStocks() {
			if(this.buy < 0) this.buy = 0;
			if(this.buy === 0) return;
			console.log('going to buy '+this.buy +' of '+this.buyStock);
			store.commit('buyStock', { amount:this.buy, stock:this.buyStock });
			this.buy = 0;
			this.buyStock = null;
		},
		sellStocks() {
			if(this.sell < 0) this.sell = 0;
			if(this.sell === 0) return;
			console.log('going to sell '+this.sell +' of '+this.sellStock);
			store.commit('sellStock', { amount:this.sell, stock:this.sellStock });
			this.sell = 0;
			this.sellStock = null;
		}
	}
});

              
            
!
999px

Console