Skip to content

Commit dd70b5b

Browse files
committed
Merge branch 'petekeller2-master'
2 parents 72c800b + 2c33acf commit dd70b5b

File tree

4 files changed

+89
-24
lines changed

4 files changed

+89
-24
lines changed

‎general-patterns/conditionals.html‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,6 @@
3636
/* alternative method 3 - binary-search-like approach
3737
* This approach is best when there are ranges of values for which to test
3838
*/
39-
if(value==0){
40-
returnresult0;
41-
}elseif(value==1){
42-
returnresult1;
43-
}elseif(value==2){
44-
returnresult2;
45-
}elseif(value==3){
46-
returnresult3;
47-
}elseif(value==4){
48-
returnresult4;
49-
}elseif(value==5){
50-
returnresult5;
51-
}elseif(value==6){
52-
returnresult6;
53-
}elseif(value==7){
54-
returnresult7;
55-
}elseif(value==8){
56-
returnresult8;
57-
}elseif(value==9){
58-
returnresult9;
59-
}else{
60-
returnresult10;
61-
}
62-
6339
if(value<6){
6440
if(value<3){
6541
if(value==0){
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<htmllang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<metacharset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/* Title: iife for loops
10+
* Description: loops that make use of counters as expected
11+
*/
12+
13+
// this will log 'regular loop 5'
14+
for(vari=0;i<5;i++){
15+
setTimeout(function(){
16+
console.log('regular loop',i);
17+
},3000);
18+
}
19+
20+
// this will log 'iife loop 1', 'iife loop 2', 'iife loop 3', 'iife loop 4'
21+
for(vari=0;i<5;i++){
22+
(function(n){
23+
setTimeout(function(){
24+
console.log('iife loop',n);
25+
},3000);
26+
})(i);
27+
}
28+
29+
// same results as the iife loop. Let isn't supported by all javascript versions
30+
for(leti=0;i<5;i++){
31+
setTimeout(function(){
32+
console.log('let loop',i);
33+
},3000);
34+
}
35+
36+
// References
37+
// http://benalman.com/news/2010/11/immediately-invoked-function-expression/
38+
</script>
39+
</body>
40+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!doctype html>
2+
<htmllang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<metacharset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/* Title: Map and Filter By Reduce
10+
* Description: Combine map and filter with reduce
11+
*/
12+
13+
// Basic example
14+
consteuro=[29.76,41.85,46.5];
15+
constabove30=euro.reduce((total,amount)=>{
16+
if(amount>30){
17+
total.push(amount);
18+
}
19+
returntotal;
20+
},[]);
21+
console.log('above30',above30);
22+
23+
// Using async/await
24+
asyncfunctionfoo(){
25+
try{
26+
varvalues=awaitgetValues();
27+
28+
returnawaitvalues.reduce(asyncfunction(values,value){
29+
values=awaitvalues;
30+
value=awaitasyncOperation(value);
31+
console.log(value);
32+
values.push(value);
33+
returnvalues;
34+
},[]);
35+
}catch(err){
36+
console.log('We had an ',err);
37+
}
38+
}
39+
40+
// References
41+
// https://medium.freecodecamp.org/reduce-f47a7da511a9
42+
// https://code.tutsplus.com/tutorials/a-primer-on-es7-async-functions--cms-22367
43+
</script>
44+
</body>
45+
</html>

‎general-patterns/parseint.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
// pattern 2
2626
/* NOTE: if you're expecting input such as “08 hello”, parseInt() will return a number, whereas the others will fail
2727
* with NaN.
28+
If the input string begins with "0", radix is eight (octal) or 10 (decimal).
29+
Exactly which radix is chosen is *implementation-dependent*.
30+
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.
2831
*/
2932
+"08"// result is 8
3033
Number("08")// 8
@@ -39,6 +42,7 @@
3942
("8.55"|0)// => 8
4043

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

0 commit comments

Comments
(0)