Skip to content

Instantly share code, notes, and snippets.

@oieioi
Last active August 29, 2015 14:23
Show Gist options
  • Save oieioi/aadaf26b2155265a2fd1 to your computer and use it in GitHub Desktop.
Save oieioi/aadaf26b2155265a2fd1 to your computer and use it in GitHub Desktop.
tower-of-babel
'use strict';
// 変数 a はblockスコープの中でも外でも再代入可能な有効な変数です。変数宣言の方法(var|let|const)のいずれを利用するべきか検討してください
var a = 5;
// 変数 b は再代入不可能な変数です。変数宣言の方法(var|let|const)のいずれを利用するべきか検討してください
const b = process.argv[2];
if (a === 5) {
// ここでの変数 c は再代入可能ですが、このblockの中でだけ有効な変数です。変数宣言の方法(var|let|const)のいずれを利用するべきか検討してください。
let c = 4;
console.log(c); // 4
// ここでの変数 b はblockの中だけで有効な変数です。変数宣言の方法(var|let|const)のいずれを利用するべきか検討してください。
let b = 8;
console.log(b); // 8
}
console.log(a); // 5
console.log(b);
try {
// ここで cに対して値を変更してみましょう。
c = 1000;
} catch (e) {
// c is not defined エラーが発生することを確認して下さい。
console.log(e);
}
class Character {
constructor(x, y){
this.x = x;
this.y = y;
this.health_ = 100;
}
damage() {
this.health_ -= 10;
}
getHealth() {
return this.health_;
}
toString() {
return `x: ${this.x} y: ${this.y} health: ${this.getHealth()}`
}
}
class Player extends Character {
constructor(x, y, name) {
super(x, y);
this.name = name;
}
move(dx, dy) {
this.x += dx;
this.y += dy;
}
toString() {
return `name: ${this.name} ${super.toString()}`
}
}
var [,,x, y, name] = process.argv;
var character = new Character(+x, +y);
character.damage();
console.log(character.toString());
var player = new Player(+x, +y, name);
player.damage();
player.move(7, 8);
console.log(player.toString());
class Character {
constructor(x, y){
this.x = x;
this.y = y;
this.health_ = 100;
}
damage() {
this.health_ -= 10;
}
getHealth() {
return this.health_;
}
toString() {
return `x: ${this.x} y: ${this.y} health: ${this.getHealth()}`
}
}
var [,,x, y] = process.argv;
var character = new Character(+x, +y);
character.damage();
console.log(character.toString());
var obj = {
[+process.argv[2] % 2 === 0 ? "even" : "odd"]: +process.argv[2],
[+process.argv[3] + +process.argv[2]]: +process.argv[3] + +process.argv[2]
};
console.log(obj);
var json = {
"name": {
"first": "Yosuke",
"family": process.argv[2]
},
"birth": {
"year": 1982,
"month": 12,
"day": process.argv[3]
}
};
// ここでデストラクチャリングを使ってname.familyをfamilyNameに。
// birth.day を birthDay に束縛してください。
let {name:{family:familyName},birth:{day:birthDay}} = json;
console.log(familyName);
console.log(birthDay);
const max = +process.argv[2];
let FizzBuzz = function*(){
let count = 0;
while (count < max) {
count += 1;
let result = count;
if (count % 15 === 0) result = 'FizzBuzz';
if (count % 3 === 0) result = 'Fizz';
if (count % 5 === 0) result = 'Buzz';
yield result;
}
}();
for (var result of FizzBuzz) {
console.log(result);
}
const max = process.argv[2];
let FizzBuzz = {
[Symbol.iterator]() {
let count = 0;
return {
next() {
count += 1;
let result = count;
if (count % 3 === 0)
result = 'Fizz';
if (count % 5 === 0)
result = 'Buzz';
if (count % 15 === 0)
result = 'FizzBuzz';
return {
done: count <= max ? false : true,
value: result
}
}
};
}
}
for (var n of FizzBuzz) {
console.log(n);
// 1
// 2
// Fizz
// 4
// Buzz
// Fizz
// 7
// 8
// Fizz
// Buzz
// 11
// Fizz
// 13
// 14
// FizzBuzz
// 16
// 17
// Fizz
// 19
// Buzz
// Fizz
// 22
// 23
// Fizz
// Buzz
// 26
// Fizz
// 28
// 29
// FizzBuzz
// ...
}
var arg1 = process.argv[2];
var arg2 = process.argv[3];
import Math from './modules-default-export-math';
console.log(Math.PI);
console.log(Math.sqrt(+arg1));
console.log(Math.square(+arg2));
const PI = 3.141592;
var _sqrt = function(s, x, last){
return x != last ? _sqrt(s, (x + s / x) / 2.0, x) : x;
};
const sqrt = function(s){
return _sqrt(s, s/2.0, 0.0);
};
const square = function(x){
return x * x;
};
export default {
PI, sqrt, square
}
var arg1 = process.argv[2];
var arg2 = process.argv[3];
import {PI, sqrt, square} from './modules-with-name-math';
console.log(PI);
console.log(sqrt(+arg1));
console.log(square(+arg2));
export const PI = 3.141592;
var _sqrt = function(s, x, last){
return x != last ? _sqrt(s, (x + s / x) / 2.0, x) : x;
};
export let sqrt = function(s){
return _sqrt(s, s/2.0, 0.0);
};
export let square = function(x) {
return x * x;
};
var arg = process.argv[2];
console.log(`Hello ${arg}`);
var args = process.argv[2].split(",");
args = args.map((arg)=> +arg);
// ここに平均を取るavg関数を作る、作る際には
// RESTパラメータを利用すること。
let avg = (...nums) => {
return nums.reduce((p, c) => p + c) / nums.length;
};
console.log(avg(...args));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment