Skip to content

Instantly share code, notes, and snippets.

View thiskevinwang's full-sized avatar

Kevin Wang thiskevinwang

View GitHub Profile
// Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
// 1. via "if else" statements
func evenOrOdd(_ number: Int) -> String {
if number % 2 == 0 {
return "Even"
} else {
return "Odd"
}
@thiskevinwang
thiskevinwang / fibonacciMemo.js
Last active January 24, 2019 02:46
Fibonacci Function with Memoization (node.js)
console.log(process.argv);
function fibonacci(n, cache) {
cache = cache || {};
if (cache[n]) { return cache[n] };
if (n <= 2) { return 1 };
return cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache);
}
@thiskevinwang
thiskevinwang / Tic-Tac-Toe.jsx
Created February 6, 2019 20:04
The final code, before the extras.
// Source: Dan Abramov
// https://codepen.io/gaearon/pen/gWWZgR?editors=0010
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
// Game component:
handleClick(i) {
const locations = [
[1, 1],
[2, 1],
[3, 1],
[1, 2],
[2, 2],
[3, 2],
// Inside Game's render method
const moves = history.map((step, move) => {
const desc = move
? "Go to move #" + move + " @ " + history[move].location
: 'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
@thiskevinwang
thiskevinwang / Tic-Tac-Toe-1.jsx
Created February 6, 2019 21:10
Solution to Task #1
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
// Inside Game's render method
// Note <button>'s child
const moves = history.map((step, move) => {
const desc = move
? "Go to move #" + move + " @ " + history[move].location
: "Go to game start";
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>
@thiskevinwang
thiskevinwang / Tic-Tac-Toe-2.jsx
Last active September 3, 2019 18:02
Solution to Task #2
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
@thiskevinwang
thiskevinwang / Tic-Tac-Toe-3.jsx
Last active January 11, 2022 15:20
Solution to Task #3
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
class Board extends React.Component {
renderSquare(i) {
return (
<Square
key={"square " + i}
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}