Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating Sequences #239

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions JavaScript/Closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Introduction

Closures have a reputation for being one of JavaScript's most difficult concepts to grasp. However, if you're already comfortable with how JavaScript deals with functions and [scope][1], you're already halfway there.
Closures have a reputation for being one of JavaScript's most difficult concepts to grasp, however, if you're already comfortable with the way JavaScript deals with functions and [scope][1], you're already halfway there.

----

Expand Down Expand Up @@ -35,9 +35,9 @@ d; // 6

The variable `a` has **global scope** so can be accessed anywhere.

The variable `b` has **local scope**, local to the function `outer()` - it can only be accessed inside of it. Similarly, variable `c` is local to the function `inner()` and can only be accessed inside of it.
The variable `b` has **local scope** - local to the function `outer()` - it can only be accessed inside it. Similarly, variable `c` is local to the function `inner()` and can only be accessed inside it.

However, due to **lexical scope**, `inner()` also has access to variables `b` and `a` because they are defined within the its parent functions - `outer()` and the global space.
However, due to **lexical scope**, `inner()` also has access to variables `b` and `a` because they are defined within the parent function - `outer()`- and also the global space.

Running this code, we set `a` and then call `outer()`, which sets `b` and calls `inner()`, which sets `c` and adds `a`, `b` and `c` together, returning a value of `6` because it has access to all 3 variables. Finally, the value of `6` is returned by `outer()` and assigned to `d`.

Expand Down
68 changes: 68 additions & 0 deletions JavaScript/Generating-sequences.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Generating-sequences

Mathematical sequences occur so frequently in nature very often this transfers into computer programming problems. For example, the
cicadas who appear periodically but only emerge after a prime number of years. Or sunflowers which have either 34, 55 or 89 petals
on their face, depending upon the size of the sunflower. Additionally, lilies and irises have three petals, buttercups and wild roses
have five, delphiniums have eight petals, all related to the Fibonacci sequence. Or simply triangular numbers in bowling!
We are surrounded by sequences which are regularly analysed within computer programming.

Being equipped to generate such sequences is vital. This article is dedicated to exploring the possible ways these sequences can be produced
within a computer program using javascript:

Prime Numbers

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps make this heading bold/bigger to improve readability


A prime number is a natural number that has exactly two distinct natural number divisors: 1 and itself. There are several prime number
sieves in circulation, however, Sieve of Eratosthenes is a simple ancient algorithm.

By iterating through an array of numbers starting at the beginning and removing all subsequent numbers that are a factor of the prime.
The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between
them that is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each
candidate number for divisibility by each prime:

function primes(num) {
var x = [];
for( var i =0; i<= num; i++) {
x[i] = true;
}
x[0]= false;
x[1]= false;
x[2]= true;
for ( var j = 2; j< num; j++) {
for (var k = 2; j*k <= num; k++) {
x[j*k] = false;
}
}

primeValues = [];

for (var i = 0; i< num; i ++) {
if (x[i] == true) {
primeValues.push(i);
}
}
return primeValues;
}

primes(20);
[2, 3, 5, 7, 11, 13, 17, 19]

Fibonnaci Sequence

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps make this heading bold/bigger to improve readability


In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values 0,1.

function fibonacci(nums) {

var fibSequence = [];

fibSequence[0] = 0;
fibSequence[1] = 1;

for (var i = 2; i < nums; i ++) {
fibSequence[i] = fibSequence[i-1] + fibSequence[i-2];
}

return fibSequence;
}

fibonacci(8);
[ 0, 1, 1, 2, 3, 5, 8, 13 ]
2 changes: 1 addition & 1 deletion JavaScript/array-map.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Array `map()` method


The `map()` method creates a new array, without altering the original, by applying a function to each element of the array. The transformation (or processing) is done by a callback function, which is specified as the first parameter of the method. Higher-order functions such as `map()`, `reduce()` or `filter()` are a shorter, more readable alternative to iterating through an array with a loop.
The `map()` method creates a new array, without altering the original, by applying a function to each element of the array. The transformation (or processing) is done by a callback function, which is specified as the first parameter of the method. Higher-order functions such as `map()`, `reduce()` or `filter()` are a shorter, more readable, alternative to iterating through an array with a loop.

## Example

Expand Down
70 changes: 70 additions & 0 deletions JavaScript/under-the-hood.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Under-the-Hood

Fundamentally all characters are a higher level abstraction of a bit number. Subsequently every character has a corresponding number value. Many programming problems are made more accessible when viewed in this way. Furthermore, it also helps to lend understanding to built-in Javascript methods such as sort(); which is able to arrange characters of the same case in alphabetical order.

Example of sort() being used:

function arrange(values) {
values.sort();
return values;
}
arrange(["China", "Germany", "United States", "Great Britain", Brazil"]);

//[ 'Brazil', 'China', 'Germany', 'Great Britain', 'United States' ]

In order to discover the unicode value of a particular character at a specified index in a string the charCodeAt() method is used:

function findCharCode(string) {
var newArray = [];
for(var i = 0; i < string.length; i++) {
newArray.push(string.charCodeAt(i));
}
return newArray;
}
findCharCode("GITHUB");

//[71, 73, 84, 72, 85, 66]

In contrast, the fromCharCode() method converts Unicode values into characters. Note: This is a static method of the String object,
and the syntax is always String.fromCharCode().

function char(value) {
var result = "";
for (var i = 0; i < value.length; i++) {
result += String.fromCharCode(value[i]);
}
return result;
}
char([97, 121, 108, 101, 114]);

//'ayler'

Unicode Characters:

D.V Char D.V Char D.V Char D.V Char
32 SPACE 56 8 80 P 104 h
33 ! 57 9 81 Q 105 i
34 " 58 : 82 R 106 j
35 # 59 ; 83 S 107 k
36 $ 60 < 84 T 108 l
37 % 61 = 85 U 109 m
38 & 62 > 86 V 110 n
39 ' 63 ? 87 W 111 o
40 ( 64 @ 88 X 112 p
41 ) 65 A 89 Y 113 q
42 * 66 B 90 Z 114 r
43 + 67 C 91 [ 115 s
44 , 68 D 92 \ 116 t
45 - 69 E 93 ] 117 u
46 . 70 F 94 ^ 118 v
47 / 71 G 95 _ 119 w
48 0 72 H 96 ` 120 x
49 1 73 I 97 a 121 y
50 2 74 J 98 b 122 z
51 3 75 K 99 c
52 4 76 L 100 d
53 5 77 M 101 e
54 6 78 N 102 f
55 7 79 O 103 g

Key: D.V = Decimal Value
1 change: 1 addition & 0 deletions READMEs
Submodule READMEs added at 305717