Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 13, 2018 16:56
Show Gist options
  • Save Anna-Myzukina/fe82449babcb939c733fb6a68ab5382c to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/fe82449babcb939c733fb6a68ab5382c to your computer and use it in GitHub Desktop.
tower-of-babel
//Exercise 11
var inputs = process.argv.slice(2);
var result = inputs.map((value, index) => {
return value[0];
}).reduce((a, b) => {
return a + b;
});
console.log(result);
//Exrcise 6
'use strict';
// This variable `a` should be accessable outside of the blog scope.
var a = 5;
// This variable `b` should not be reassignable.
const b = process.argv[2];
if (a === 5) {
// This variable `c` should only be valid in this block.
let c = 4;
console.log(c); // 4
// This variable `b` should only be valid in this block.
let b = 8;
console.log(b); // 8
}
console.log(a); // 5
console.log(b);
try {
// Trying to change the value of `c`
c = 1000;
} catch (e) {
// but an `c is not defined` error should occur.
console.log(e);
}
//Exrcise 6
'use strict';
// This variable `a` should be accessable outside of the blog scope.
var a = 5;
// This variable `b` should not be reassignable.
const b = process.argv[2];
if (a === 5) {
// This variable `c` should only be valid in this block.
let c = 4;
console.log(c); // 4
// This variable `b` should only be valid in this block.
let b = 8;
console.log(b); // 8
}
console.log(a); // 5
console.log(b);
try {
// Trying to change the value of `c`
c = 1000;
} catch (e) {
// but an `c is not defined` error should occur.
console.log(e);
}
/*Exercise 2
Rewrite the following class in the new ES6 class syntax
*/
class Character {
constructor(x, y) {
this.x = x;
this.y = y;
this.health_ = 100;
}
damage() {
this.health_ = this.health_ - 10;
}
getHealth() {
return this.health_;
}
toString() {
return "x: " + this.x + " y: " + this.y + " health: " + this.getHealth();
}
}
var x = process.argv[2];
var y = process.argv[3];
var character = new Character(+x, +y);
character.damage();
console.log(character.toString());
/*Exercise 3
Rewrite the classes that are written below in the prototype and util.inherit fashion with the new ES6 syntax
*/
class Character {
constructor(x, y) {
this.x = x;
this.y = y;
this.health_ = 100;
}
damage() {
this.health_ = 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.call(this);
}
}
var x = process.argv[2];
var y = process.argv[3];
var name = process.argv[4];
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());
//Exercise 7
var evenOrOdd = +process.argv[2];
var evenOrOddKey = evenOrOdd % 2 === 0 ? "even" : "odd";
var sum = +process.argv[3] + evenOrOdd;
var getEvenOrOddKey = function(eOrO) {
return evenOrOdd % 2 === 0 ? "even" : "odd";
};
var obj = {
[getEvenOrOddKey(evenOrOdd)]: evenOrOdd,
[sum]: sum
};
console.log(obj);
//Exercise 10
var json = {
"name": {
"first": "Yosuke",
"family": process.argv[2]
},
"birth": {
"year": 1982,
"month": 12,
"day": process.argv[3]
}
};
var {
"name": {
"family": familyName
}
} = json;
var {
"birth": {
"day": birthDay
}
} = json;
console.log(familyName);
console.log(birthDay);
//Exercise 9
const max = process.argv[2];
let FizzBuzz = function *() {
let number = 0;
while (number < max) {
number++;
if (number % 15 === 0) {
yield 'FizzBuzz';
} else if (number % 3 === 0) {
yield 'Fizz';
} else if (number % 5 === 0) {
yield 'Buzz';
} else {
yield number;
}
}
}();
for (var n of FizzBuzz) {
console.log(n);
}
//Exercise 8
const max = process.argv[2];
let FizzBuzz = {
[Symbol.iterator]() {
let number = 0;
return {
next() {
number++;
if (number <= max) {
return {
done: false,
value: (function() {
if (number % 15 === 0) {
return 'FizzBuzz';
} else if (number % 3 === 0) {
return 'Fizz';
} else if (number % 5 === 0) {
return 'Buzz';
} else {
return number;
}
}())
}
} else {
return {
done: true
}
}
}
}
}
}
for (var n of FizzBuzz) {
console.log(n);
}
//Exercise 4
var arg1 = process.argv[2];
var arg2 = process.argv[3];
import {PI, sqrt, square} from './Math';
console.log(PI);
console.log(sqrt(+arg1));
console.log(square(+arg2));
//Exercise 5
var arg1 = process.argv[2];
var arg2 = process.argv[3];
import Main from './Math';
console.log(Main.PI);
console.log(Main.sqrt(+arg1));
console.log(Main.square(+arg2));
//Exercise 4
export const PI = 3.141592;
var _sqrt = function(s, x, last){
return x != last ? _sqrt(s, (x + s / x) / 2.0, x) : x;
};
export function sqrt(s){
return _sqrt(s, s/2.0, 0.0);
};
export function square(x) {
return x * x;
};
//Exercise 5
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 : PI,
_sqrt: _sqrt,
sqrt: sqrt,
square: square
};
/*Exercise 1
Create a javascript program that takes the the first command-line argument and
outputs it right after a "Hello " String using ES6 template strings.
*/
var arg = process.argv[2];
console.log(`Hello ${arg}`);
//Exercise 12
var args = process.argv[2].split(",");
args = args.map((arg) => +arg);
// write a function called `avg` here that calculates the average
var avg = function(...args) {
let sum = args.reduce((sum, n) => sum + n);
return sum / args.length;
};
console.log(avg(...args));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment