-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes6-excercises.js
More file actions
73 lines (50 loc) · 1.92 KB
/
es6-excercises.js
File metadata and controls
73 lines (50 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//ES6 Features Excercise
//Template Strings Examples
let tempStrings = ["good", "great", "fantastic", "the best thing since sliced bread"];
let stringUsage = _.random(1, 100);
console.log(
`Template strings are ${tempStrings[_.random(0,_.size(tempStrings) - 1)]}!
They take into account newlines.
I use template strings ${stringUsage} times a day.
Thats ${stringUsage * 7} times a week, ${stringUsage * 28} times a month and ${stringUsage * 365.2422} times a year!`
);
//Const example calculate wavelength based on frequency
let freq = 20;
function waveLengthCalc(freq) {
const SPEED_OF_SOUND = 340;
return [SPEED_OF_SOUND / freq, (SPEED_OF_SOUND / freq) * 100];
}
if (freq > 340) {
console.log(`
The wavelength of a ${freq}Hz tone is ${waveLengthCalc(freq)[1]} centimeters.`);
} else {
console.log(`
The wavelength of a ${freq}Hz tone is ${waveLengthCalc(freq)[0]} meters.`);
}
//Object Initiliazer and Destructuring Shorthand
function catAges (birthYear){
let age = 2017 - birthYear;
let humanAge;
if (age < 1) {
humanAge = age * 15;
} else if (age > 1 && age < 2) {
humanAge = (age - 1) * 24;
} else {
humanAge = 24 + ((age - 2) * 4);
}
//Initiliaze object
return {birthYear, age, humanAge};
}
//Destructure object
let {birthYear, age, humanAge} = catAges(2010);
console.log(`A cat born in ${birthYear} would be ${age} years old in cat years, and ${humanAge} in human years.`);
/* Rest and spread
var enemyType = ["Giant Squid", "Four-headed Piranha", "7ft Duck", "Robotic Sloth", "Death-claw", "Zombie-Pigman"];
var weaponType = ["Tooth-pick", "Water-Gun", "the Eye of Sauron", "tourettes", "Searing Shish-kebab", "a handful of rocks"]
function createEnemyGroup(groupName,...members) {
for (let i in members) {
console.log(`${members[i]} is a ${enemyType[_.random(0, (enemyType - 1))]} yielding ${weaponType[_.random(0, (weaponType - 1))]}`);
}
}
createEnemyGroup("The Dans", "Dan", "Danise", "Jordon Holt");
*/