Skip to content

Instantly share code, notes, and snippets.

View niinpatel's full-sized avatar
💭
goofing off

Nitin Patel niinpatel

💭
goofing off
View GitHub Profile
import React, { Component } from 'react';
import './App.css';
import ResultComponent from './components/ResultComponent';
import KeyPadComponent from "./components/KeyPadComponent";
class App extends Component {
constructor(){
super();
this.state = {
import React, { Component } from 'react';
import './App.css';
import ResultComponent from './components/ResultComponent';
import KeyPadComponent from "./components/KeyPadComponent";
class App extends Component {
constructor(){
super();
this.state = {
import React, {Component} from 'react';
class KeyPadComponent extends Component {
render() {
return (
<div className="button">
<button name="(" onClick={e => this.props.onClick(e.target.name)}>(</button>
<button name="CE" onClick={e => this.props.onClick(e.target.name)}>CE</button>
<button name=")" onClick={e => this.props.onClick(e.target.name)}>)</button>
import React, {Component} from 'react';
class ResultComponent extends Component {
render() {
let {result} = this.props;
return (
<div className="result">
<p>{result}</p>
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
let applications = [];
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
console.log(simulateGame(1000, toSwitch = true)); // play 1000 times, switch the door. Print how many times won.
console.log(simulateGame(1000, toSwitch = false)); // play 1000 times, don't switch the door. Print how many times won.
/* Output of first line will be close to 666 while that of second will be close to 333. */
function simulateGame(num, toSwitch) {
let gamesWon = 0;
for(let i = 0; i < num; i++){
gamesWon += playMontyHall(toSwitch)
}
return gamesWon
}
function playMontyHall(toSwitch) {
let carIsIn = Math.floor(Math.random() * 3);
let doorSelected = Math.floor(Math.random() * 3);
let revealedDoor = [0,1,2].find((door) => door !== carIsIn && door !== doorSelected);
if(toSwitch){
return carIsIn === [0,1,2].find((door) => door !== doorSelected && door !== revealedDoor)
setTimeout(() => console.log('this code is executed asynchronously'), 0)
@niinpatel
niinpatel / asyncmodule.js
Last active June 9, 2018 20:11
Asynchronous Map Function
/**
* Asynchronous Map Function via async module
*/
const Async = require('async');
Async.map([1,2,3], double, (err, results) => {
if(err) console.log(err);
console.log(results)
} );