Skip to content

Commit

Permalink
Merge branch 'petekeller2-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
chuanxshi committed Jun 10, 2018
2 parents 72c800b + 2c33acf commit dd70b5b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
24 changes: 0 additions & 24 deletions general-patterns/conditionals.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,6 @@
/* alternative method 3 - binary-search-like approach
* This approach is best when there are ranges of values for which to test
*/
if (value == 0) {
return result0;
} else if (value == 1) {
return result1;
} else if (value == 2) {
return result2;
} else if (value == 3) {
return result3;
} else if (value == 4) {
return result4;
} else if (value == 5) {
return result5;
} else if (value == 6) {
return result6;
} else if (value == 7) {
return result7;
} else if (value == 8) {
return result8;
} else if (value == 9) {
return result9;
} else {
return result10;
}

if (value < 6) {
if (value < 3) {
if (value == 0) {
Expand Down
40 changes: 40 additions & 0 deletions general-patterns/iife-for-loop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: iife for loops
* Description: loops that make use of counters as expected
*/

// this will log 'regular loop 5'
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log('regular loop', i);
}, 3000);
}

// this will log 'iife loop 1', 'iife loop 2', 'iife loop 3', 'iife loop 4'
for (var i = 0; i < 5; i++) {
(function(n) {
setTimeout(function() {
console.log('iife loop', n);
}, 3000);
})(i);
}

// same results as the iife loop. Let isn't supported by all javascript versions
for (let i = 0; i < 5; i++) {
setTimeout(function() {
console.log('let loop', i);
}, 3000);
}

// References
// http:https://benalman.com/news/2010/11/immediately-invoked-function-expression/
</script>
</body>
</html>
45 changes: 45 additions & 0 deletions general-patterns/map-and-filter-by-reduce.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Map and Filter By Reduce
* Description: Combine map and filter with reduce
*/

// Basic example
const euro = [29.76, 41.85, 46.5];
const above30 = euro.reduce((total, amount) => {
if (amount > 30) {
total.push(amount);
}
return total;
}, []);
console.log('above30', above30);

// Using async/await
async function foo() {
try {
var values = await getValues();

return await values.reduce(async function(values, value) {
values = await values;
value = await asyncOperation(value);
console.log(value);
values.push(value);
return values;
}, []);
} catch (err) {
console.log('We had an ', err);
}
}

// References
// https://medium.freecodecamp.org/reduce-f47a7da511a9
// https://code.tutsplus.com/tutorials/a-primer-on-es7-async-functions--cms-22367
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions general-patterns/parseint.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
// pattern 2
/* NOTE: if you're expecting input such as “08 hello”, parseInt() will return a number, whereas the others will fail
* with NaN.
If the input string begins with "0", radix is eight (octal) or 10 (decimal).
Exactly which radix is chosen is *implementation-dependent*.
ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
*/
+"08" // result is 8
Number("08") // 8
Expand All @@ -39,6 +42,7 @@
("8.55"|0) // => 8

// References
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
// http:https://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/
</script>
</body>
Expand Down

0 comments on commit dd70b5b

Please sign in to comment.