loading...
Cover image for Learn Data Structure and Algorithm in JavaScript | Part 04

Learn Data Structure and Algorithm in JavaScript | Part 04

edisonnpebojot profile image Edison Pebojot(๐Ÿ‘จโ€๐Ÿ’ป) Updated on ใƒป19 min read


Prerequisite (โœ‹๐Ÿ˜)

If you're reading this article right now, please considered to read our Part 01: Big-O Notation, Part 02: JavaScript Unique Part, and Part 03: JavaScript Numbers

Part Table of Contents Description
01 Big-O Notation By the end of this chapter, you will understand how to analyze an implementation of an algorithm with respect to both time (execution time) and space (memory consumed).
02 JavaScript Unique Parts Big-O is important for analyzing and comparing the efficiencies of algorithms. The analysis of Big-O starts by looking at the code, and, applying the rules, applying the rules is because to simplify the Big-O notation linear or quadratic rule is not enough.
03 JavaScript Numbers This part 3 will focus on JavaScript number operations, number representation, Number objects, common number algorithms, and random number generation.
04 JavaScript Strings This part 4 will focus on strings, JavaScript String object, and the String objectโ€™s built-in functions. You will learn how to access, compare, decompose, and search strings for commonly used real-life purposes. In addition, the chapter will explore string encoding, decoding, encryption, and decryption.
05 JavaScript Arrays As a JavaScript developer, you will use the array often; it is the most commonly used data structure. Arrays in JavaScript come with a lot of built-in methods. By the end of this part, you will understand arrays and choose the right method
06 JavaScript Object This part will focus on what JavaScript objects are, how they are declared, and how their properties can be changed. In addition, this part will cover how JavaScript classes are implemented using prototypal inheritance. Also this part will be short.
07 JavaScript Memory Management A variable takes up some memory. In C, the programmer allocate and deallocate memory manually. In contrast, modern JavaScript engines have garbage collectors that delete unused variables. However, there are pitfalls(unexpected) that developers can fall into(โ—) This part will show these unexpected and present techniques to help the garbage collector minimize the memory problems.
08 Recursion This part 8 introduces the concept of recursion and recursive algorithms(Remember they are different, we will discuss them later(๐Ÿ˜‰)). We will also discuss the definition of recursion and fundamental rules for recursive algorithms.
09 Sets This part focuses on the concepts of sets from both a mathematical definition and on the implementation. Also, Common set operations, as well as their implementations, are covered in great detail (๐Ÿ’ก).
10 Searching and Sorting This part 10 focuses on searching and sorting for arrays. By the end of this part 10, you will understand how to use sorting and searching algorithms for arrays. Also, this article is a bit complicated for beginners, so as much as possible the visual aids is your friend (๐Ÿ‘€). (๐Ÿ˜ƒ)
11 Hash Tables A hash table is a fixed-sized data structure in which the size is defined at the start. This part 11 explains how hash tables work, and the method of generating a unique key. By the end of this part 11, you will understand various hashing techniques and know how to implement a hash table. (๐Ÿ˜ƒ)
12 Stacks and Queues This part 12 covers stacks and queues(pronounce as kyooz (๐Ÿ”ˆ)) not (kwewe) okay? hehehe (๐Ÿ˜…); both are data structures used in the implementation of complex data structures. You'll learn what the stacks and queues are, how they're used, when they're used, and how to implement them (๐Ÿ˜ƒ) Let's go! (๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ)
13 Linked Lists A linked list is a data structure in which each node (or element) points to another node. Unlike arrays, which have a fixed size, a linked list is a dynamic data structure. By the end of this part 13, you will understand how to implement and work with linked lists. And oh! (๐Ÿ˜ฎ) There are two types of linked lists: singly (โžก๏ธ) and doubly (โ†”๏ธ). Letโ€™s examine the singly linked list first.(๐Ÿ˜ƒ) Let's go! (๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ)
14 Caching Caching is the process of storing data into temporary memory so that it can be easily retrieved for later use if it is required again. As an example, a database keeps data cached to avoid re-reading the hard drive, and a web browser caches web images to avoid re-downloading. In this part 14, two caching techniques will discussed: LFU and LRU caching.
15 Trees A general tree data structure is composed of nodes with children nodes. The top node is called the root node. This part 15 will explore different types of trees such as binary trees, binary search trees, and self-balancing binary search trees. First, this part 15 will cover what trees are and how they are structured. Then, it will cover methods of traversing(crossing or taking a zigzag path) the tree data structure in detail. Finally, you will learn about binary search trees and self-balancing binary search trees to understand how to store searchable data. (๐Ÿ˜ƒ)
16 Heaps A heap is an important data structure that returns the highest or lowest element in O(1) time. This part 16 will focus on explaining how heaps are implemented as well as how to work with them. One example is heap sort, which is a sorting algorithm based on heaps.
17 Graphs In this Part 17, you will learn graph basics, including fundamental terminology and graph types. The Part 17 will also cover working with these different graph types and methods of representing graphs in data structures. Finally, algorithms for traversing, searching, and sorting graphs are explored to solve problems such as finding the shortest path between two graph nodes. (๐Ÿ‘)
18 Advance Strings Part 18 will cover more complex string algorithms than covered in the previous section. Now that you have heard of certain other data models or structures, they should be easier to comprehend. Specifically, Part 18 will focus on string searching algorithms. (๐Ÿ˜‰)
19 Dynamic Programming Dynamic programming involves breaking down problems into their subproblems. Solving the subproblems and saving those results into memory to access them whenever a repeated problem needs to be solved, the algorithmic complexity decreases significantly (โฌ‡๏ธ). To explain dynamic programming, letโ€™s re examine the Fibonacci sequence that was discussed in Part 8. Then Part 19 will cover the rules of dynamic programming and walk you through some examples to make the concepts more concrete. (๐Ÿ˜‰)
20 Bit Manipulation Bit manipulation is an advanced topic that JavaScript developers typically do not need to know. However, you should learn a bit about bit manipulation if you want to implement high-performance server-side code. Understanding bit manipulation requires some knowledge of digital logic. Any introductory course in discrete math or circuits would be helpful to understand these concepts.

Part 04: JavaScript Strings(๐Ÿ˜ฑ ๐Ÿ”ฅ ๐Ÿ” )

Alt Text

This part 4 will focus on strings, JavaScript String object, and the String objectโ€™s built-in functions. You will learn how to access, compare, decompose, and search strings for commonly used real-life purposes. In addition, the chapter will explore string encoding, decoding, encryption, and decryption. By the end of this part, you will understand how to effectively work with JavaScript strings and have a fundamental understanding of string encoding and encryption. (๐Ÿ”’)

JavaScript String Primitive (๐Ÿ”ค)

Alt text

JavaScriptโ€™s String primitive comes with various common string functions.

String Access (๐Ÿ”“)

For accessing characters, you use .chartAt():

'dog'.charAt(1); // returns "o"

.charAt( index ) takes an index (which starts at 0) and returns the character at that index location in the string.

For multiple-character access, you can use .substring( startIndex, endIndex ), which will return the characters between indices:

console.log('YouTube'.substring(1,2)) // returns 'o' console.log('YouTube'.substring(3,7)) // returns 'tube'

If you do not pass a second parameter (startIndex, endIndex), it will return all the character values:

'YouTube'.substring(0); // returns 'Youtube'

String Comparison ( == )

Programming languages have a function that allows you to compare strings. In JavaScript, this can be done simply by using less-than (<) and greater-than (>) operators:

'a' < 'b' // prints 'true'

This can be really useful for comparing strings when sorting algorithms, which is covered later in part 10. However, if you are comparing two strings of different lengths, it starts comparing from the start of the string:

'abc' < 'b' // prints 'true'

Since add is smaller than b, abc < b evaluates to true:

'add' < 'ab' // prints 'false'

In this example, a d d and a b are compared. This is the
same as comparing ad with ab:

'add'<'ab' == 'ad'<'ab' // prints 'true'

String Search (๐Ÿ”๐Ÿ”๐Ÿ”)

To find a specific string within a string, you can use .indexOf ( searchValue [ , fromIndex ] ) . This takes the string to be searched as well as the starting index for the search. It returns the position of the
matching string, but if found nothing, then -1 (negative one) is returned:

// Return positive numbers console.log('Red Dragon'.indexOf('Red')) // returns 0 console.log('Red Dragon'.indexOf('Dragon', 0)) // returns 4 console.log('Red Dragon'.indexOf('Dragon', 4)) // returns 4 console.log('Red Dragon'.indexOf("", 9)) // returns 9 // Return negative number console.log('Red Dragon'.indexOf('RedScale')) // returns -1

To search string inside a larger string, simply check -1 from .indexOf :

let existsInString=(stringValue, search)=> stringValue.indexOf(search) !== -1; console.log(existsInString('Let say I am the largest string', 'r')); // prints 'true'; console.log(existsInString('Let say I am the largest string', 'b')); // prints 'false';

In the following example, the occurrences of the character a will be counted:

// This will count how many 'a' inside the string ((str="He's my king from this day until his last day")=>Array.from(str).map((v,i)=>v=='a'?str.indexOf('a')!==-1?0+1:0:0).filter((v=>v)).length)()

Finally, startsWith returns true (boolean) if the string starts with the input, and endsWith checks whether the string ends with the input:

console.log('Red Dragon'.startsWith('Red')) // returns true console.log('Red Dragon'.endsWith('Dragon')) // returns true console.log('Red Dragon'.startsWith('Dragon')) // returns false console.log('Red Dragon'.endsWith('Red')) // returns false

String Decomposition (๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ)

For decomposing a string into parts, you can use .split( separator ) . It creates an array of substrings:

'chicken,noodle,soup,broth'.split(","); // ["chicken", "noodle", "soup", "broth"]

Passing an empty separator will create an array of:

'chicken'.split(""); // // ["c", "h", "i", "c", "k", "e", "n"]

The string can be turned into an array to easily iterate through them:

'chicken'.split("").forEach(v=>console.log(v))

String Replace (โœ‚๏ธโœ‚๏ธโœ‚๏ธ)

.replace( string , replaceString ) replaces a string with another string:

"Wizard of Oz".replace("Wizard","Witch"); // "Witch of Oz"

Regular Expressions (๐Ÿ˜›๐Ÿ˜ฒ๐Ÿ˜ฑ๐Ÿ˜†)

Alt text

Regular expressions (or regexes) define a search pattern. JavaScript comes with object RegExp, which is used for regular expressions.

The constructor for RegExp object takes two parameters: the regular expression and the optional match settings, as shown here:

i. Perform case-insensitive matching
g. Perform a global match (find all matches rather than stopping after
first match)
m. Perform multiline matching
Enter fullscreen mode Exit fullscreen mode

RegExp has two functions:

  • search(): returns the index of the match.
  • match(): returns all the matches.

JavaScript String object also has the following two regex-related functions that accept the RegExp object:

  • exec(): returns the first match.
  • test(): returns true or false.

Basic Regex (๐Ÿ‘Œ๐Ÿ‘Œ๐Ÿ‘Œ)

Here are the basic regex rules:

  • ^: Indicates the start of a string
  • \d: Finds any digit
  • [abc]: Finds any character between the brackets
  • [^abc]: Finds any character not between the brackets
  • [0-9]: Finds any digit between the brackets
  • [^0-9]: Finds any digit not between the brackets
  • (x|y): Finds any of the alternatives

The following returns index 11, which is the index of the character D, which is the first character of the matched regex:

"JavaScript DataStructures".search(/DataStructures/) // prints '11'

Commonly Used Regexes (๐Ÿ†—๐Ÿ†—๐Ÿ†—)

Regexes are helpful for checking the validity of user input in JavaScript. One common type of input check is to validate whether it has any numeric characters. The following are five regexes that developers often use:

Any Numeric Characters: /\d+/ (๐Ÿ”ข๐Ÿ” )

// Number console.log(/\d+/.test("123")) // true // Number and Letter console.log(/\d+/.test("33asd")) // true console.log(/\d+/.test("5asdasd")) // true // Only letter console.log(/\d+/.test("asdasd")) // false

Only Numeric Characters: /\d+$/ (1๏ธโƒฃ2๏ธโƒฃ3๏ธโƒฃ4๏ธโƒฃ)

// Only Numeric console.log(/^\d+$/.test("123")) // true // Numeric and Letter console.log(/^\d+$/.test("123a")) // false // Only Letter console.log(/^\d+$/.test("a")) // false

Floating Numeric Characters: /^[0-9].[0-9][1-9]+$/ (1๏ธโƒฃ2๏ธโƒฃ . 3๏ธโƒฃ4๏ธโƒฃ)

var regex=/^[0-9].[0-9][1-9]+$/; // No Floating point console.log(regex.test("12")) // false // With Floating point console.log(regex.test("12.2")) // true

Only Alphanumeric Characters: /[a-zA-Z0-9] / (๐Ÿ”ค๐Ÿ”ค๐Ÿ”ค)

var regex = /[a-zA-Z0-9]/; // With Alphanumeric console.log(regex.test("somethingELSE")); // true console.log(regex.test("hello")); // true console.log(regex.test("112a")); // true console.log(regex.test("112")); // true // Without Alphanumeric console.log(regex.test("^")); // false

Query String: /([^?=&]+)(=([^&]*))/ (๐Ÿ”Ž๐Ÿ”Ž๐Ÿ”Ž)

In web applications, web URLs pass parameters for routing or database query. For example, for the URL: http://your.domain/product.aspx?category=4& product_id=2140&query=lcd+tv, the URL might respond to a back-end SQL query like
the following:

SELECT LCD, TV FROM database WHERE Category = 4 AND Product_id=2140
Enter fullscreen mode Exit fullscreen mode

To parse these parameters, regexes can be useful:

var queryString = {}; 'http://your.domain/product.aspx?category=4&product_id=2140&query=lcd+tv'.replace( new RegExp ("([^?=&]+)(=([^&]*))?" , "g" ), function ($0, $1, $2, $3) { queryString[$1] = $3; } ); console.log('ID: ' + queryString['product_id' ]); // ID: 2140 console.log('Name: ' + queryString['product_name' ]); // Name: undefined console.log('Category: ' + queryString['category' ]); // Category: 4

Encoding (๐Ÿ“„๐Ÿ“„๐Ÿ“„)

Alt text

Encoding is a general concept in computer science for transmission or storage. All computer file types are encoded in specific structures. For example, when you upload a PDF, the encoding may look like this:

1. JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1Bh
Z2VzIDMgMCBS\
2. ID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+PgplbmR
vYmoKMyAwIG9i\
3. ago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCl0KL0NvdW50IDEKL1Jlc291cmNlcyA8
PAovUHJvY1Nl\
4. dCA0IDAgUgovRm9udCA8PCAKL0YxIDggMCBSCj4+
Cj4+Ci9NZWRpYUJveCBbMC4wMDAgMC4w
MDAgNjEyLjAw\
5. MCA3OTIuMDAwXQogPj4KZW5kb2JqCjQgMCBvYmoKWy9QREYgL1RleHQgXQplbmRvYmoKNSAw
IG9iago8PAov\
6. Q3JlYXRvciAoRE9NUERGKQovQ3JlYXRpb25EYXRlIChEOjIwMTUwNzIwMTMzMzIzKzAyJzAw
JykKL01vZERh\
7. dGUgKEQ6MjAxNTA3MjAxMzMzMjMrMDInMDAnKQo+PgplbmRvYmoKNiAwIG9iago8PCAvVHlw
ZSAvUGFnZQov\
8. UGFyZW50IDMgMCBSCi9Db250ZW50cyA3IDAgUgo+PgplbmRvYmoKNyAwIG9iago8PCAvRmls
dGVyIC9GbGF0\
9. ZURlY29kZQovTGVuZ3RoIDY2ID4+CnN0cmVhbQp4nOMy0DMwMFBAJovSuZxCFIxN9AwMzRTM
DS31DCxNFUJS\
10. FPTdDBWMgKIKIWkKCtEaIanFJZqxCiFeCq4hAO4PD0MKZW5kc3RyZWFtCmVuZG9iago4IDA
gb2JqCjw8IC9U\
11. eXBlIC9Gb250Ci9TdWJ0eXBlIC9UeXBlMQovTmFtZSAvRjEKL0Jhc2VGb250IC9UaW1lcy1
Cb2xkCi9FbmNv\
12. ZGluZyAvV2luQW5zaUVuY29kaW5nCj4+CmVuZG9iagp4cmVmCjAgOQowMDAwMDAwMDAwIDY
1NTM1IGYgCjAw\
13. MDAwMDAwMDggMDAwMDAgbiAKMDAwMDAwMDA3MyAwMDAwMCBuIAowMDAwMDAwMTE5IDAwMDA
wIG4gCjAwMDAw\
14. MDAyNzMgMDAwMDAgbiAKMDAwMDAwMDMwMiAwMDAwMCBuIAowMDAwMDAwNDE2IDAwMDAwIG4
gCjAwMDAwMDA0\
15. NzkgMDAwMDAgbiAKMDAwMDAwMDYxNiAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDkKL1J
vb3QgMSAwIFIK\
16. L0luZm8gNSAwIFIKPj4Kc3RhcnR4cmVmCjcyNQolJUVPRgo=.....
Enter fullscreen mode Exit fullscreen mode

This is a Base64-encoded PDF string . Data like this is often passed to the server when a PDF file is uploaded. (๐Ÿ’ก๐Ÿ’ก)

Base64 Encoding (6๏ธโƒฃ4๏ธโƒฃ6๏ธโƒฃ4๏ธโƒฃ6๏ธโƒฃ4๏ธโƒฃ)

The .btoa() ( See on w3schools ) function creates a Base64-encoded ASCII string from a string. Each character in the string is treated as a byte ( 8 bits: eight 0 and 1 s).

The .atob() ( See on w3schools ) function decodes a string that has been encoded.

For example, the string โ€œhello I love learning to computer programโ€ in a Base64-encoded string looks like this:

aGVsbG8gSSBsb3ZlIGxlYXJuaW5nIHRvIGNvbXB
1dGVyIHByb2dyYW0
Enter fullscreen mode Exit fullscreen mode

See my Glitch sample and click button below named (View App):

Note(๐Ÿ“): Click the script.js file and experiment with the code. (๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š)

Learn more about Base64 at https://en.wikipedia.org/wiki/Base64

In computer science, Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding. Each non-final Base64 digit represents exactly 6 bits of data. Three 8-bit bytes can therefore be represented by four 6-bit Base64 digits.

String Shortening (๐Ÿ“๐Ÿ“๐Ÿ˜)

Have you ever wondered how URL-shortening sites such as Bit.ly work? A simplified URL compression algorithm follows a certain structure as shown here for www.google.com is an example:

  1. The database creates a unique ID for the URL:
Long ID URL
11231230 www.google.com
Figure 4-1. Database
  1. The ID is shortened into a string with Base62 encoding:
Long ID URL Short ID
11231230 www.google.com VhU2
Figure 4-2. Database after shortening

For the shortening part, there are 62 possible letters and numbers, consisting of 26 lowercase letters, 26 uppercase letters, and 10 numbers (0 to 9). The following algorithm can be used:

var DICTIONARY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""); function encodeId(num) { var base = DICTIONARY.length; var encoded = ""; if (num === 0) { return DICTIONARY[0]; } while (num > 0) { encoded += DICTIONARY[(num % base)]; num = Math.floor(num / base); } return reverseWord(encoded); } function reverseWord(str) { var reversed = ""; for (var i = str.length - 1; i >= 0; i--) { reversed += str.charAt(i); } return reversed; } function decodeId(id) { var base = DICTIONARY.length; var decoded = 0; for (var index = 0; index < id.split("").length; index++) { decoded = decoded * base + DICTIONARY. indexOf(id.charAt(index)); } return decoded; } console.log(encodeId(11231230)); // prints 'VhU2' console.log(decodeId('VhU2')); // prints '11231230'

Encryption (๐Ÿ”‘๐Ÿ”’๐Ÿ”“)

Alt text

Figure 4-3. SSL warning

Have you ever seen the warning in Figure 4-3 in the Google Chrome browser (๐Ÿ˜โ“โ“). This likely means the web site you are trying to access does not have the proper Secure Sockets Layer (SSL) certificate.

Alt Text

Figure 4-4. TSL process

TSL is a standard security for establishing an encrypted link between the server and the client. In this process, asymmetric encryption is used by the server. The browser only uses symmetric encryption :

  1. The server sends its asymmetric public key to the browser.
  2. The browser creates a symmetric key for the current session.
  3. The server decrypts the browserโ€™s session via its private key and retrieves the session key.
  4. Both systems have the session key and will use this to transmit data securely.

This is secure because only the browser and the server know the session key. If the browser was to connect to the same server the next day, a new session key would be created.

The SSL warning message is a sign that the browser and server may not be encrypting the data on that connection. The most commonly used public-key encryption algorithm is the RSA algorithm.

RSA Encryption (๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘)

RSA is an encryption algorithm based on factoring large integers. In RSA, two large prime numbers and a supplementary value are generated as public key. Anyone can use the public key to encrypt a message, but only those with the prime factors can decode the message. (๐Ÿ’ก๐Ÿ’ก๐Ÿ’ก)

There are three phases in the process: key generation, encryption, and decryption.

  1. Key generation: The public key and private key are generated. The construction method of the keys
    generated should be secret.

  2. Encryption: The message can be encrypted via public key

  3. Decryption: Only the private key can be used to decrypt the
    message.

Hereโ€™s an overview of the algorithm:

1. Select two (usually large) prime numbers, pp and qq .
a. The product of pp and qq is denoted as nn .
b. The product of (pโˆ’1)\lparen p - 1 \rparen and (qโˆ’1)\lparen q - 1 \rparen is denoted as phi.
2. Choose two exponents, ee and dd .
a. ee is typically 33 . Other values greater than 22 can be used.
b. dd is a value such that (eร—d)%ย phi=1(e ร— d) \%\ phi = 1 .

Encryption process is as shown:

m - message:
m^e % n = c
c - encrypted message
Enter fullscreen mode Exit fullscreen mode

Decryption process is as shown:

c^d % n = m
Enter fullscreen mode Exit fullscreen mode

This is the implementation of calculating dd :

function modInverse(e, phi) { var m0 = phi, t, q; var x0 = 0, x1 = 1; if (phi == 1) return 0; while (e > 1) { // q is quotient q = Math.floor(e / phi); t = phi; // phi is remainder now, process same as // Euclid's algo phi = e % phi, e = t; t = x0; x0 = x1 - q * x0; x1 = t; } // Make x1 positive if (x1 < 0) x1 += m0; return x1; } modInverse(7, 40) // 23

Key pairs of a public key and a private key also need to be generated. Letโ€™s pick 5 and 11 as the primes:

function modInverse(e, phi) { var m0 = phi, t, q; var x0 = 0, x1 = 1; if (phi == 1) return 0; while (e > 1) { // q is quotient q = Math.floor(e / phi); t = phi; // phi is remainder now, process same as // Euclid's algo phi = e % phi, e = t; t = x0; x0 = x1 - q * x0; x1 = t; } // Make x1 positive if (x1 < 0) x1 += m0; return x1; } function isPrime(n){ var prime_numbers=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] for(let i of prime_numbers){ if(n===i){ return true } } } function RSAKeyPair(p, q) { // Need to check that they are primes if (!(isPrime(p) && isPrime(q))) return; // Need to check that they're not the same if (p == q) return; var n = p * q, phi = (p - 1) * (q - 1), e = 3, d = modInverse(e, phi); // Public key: [e,n], Private key: [d,n] return [[e, n], [d, n]] } RSAKeyPair(5,11) //Public key: [3,55], Private key: [27,55]

Let's use the secret message as number 2, higher than that will result to incorrect decryption. JavaScript internally uses floating point operation. If the numbers get big you should not be surprised if the answer is incorrect:

Encryption:

// m - secret message: 2 // Always remember our secret message // m^e % n = c // 2^3 % 55 = 8 2**3 % 55 // You can also Math.pow(base,exponent) % modulus // Now the value is 8. Use the value of 8 to decryption

Decryption:

// c^d % n = m 8**27 % 55 // You can also use Math.pow(base,exponent) % modulus

Complete: Encryption and Decryption

function modInverse(e, phi) { var m0 = phi, t, q; var x0 = 0, x1 = 1; if (phi == 1) { return 0; } while (e > 1) { // q is quotient q = Math.floor(e / phi); t = phi; // phi is remainder now, process same as // Euclid's algo phi = e % phi // 3 % 40 e = t; // e = 40 t = x0; // t = 0 x0 = x1 - q * x0; // 1-0|13|3 x 0 x1 = t; // 0 } // Make x1 positive if (x1 < 0) { x1 += m0; } return x1; } function isPrime(n){ var prime_numbers=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] for(let i of prime_numbers){ if(n===i){ return true } } } function RSAKeyPair(p, q) { // Need to check that they are primes if (!(isPrime(p) && isPrime(q))) { return; } // Need to check that they're not the same if (p==q) { return; } var n = p * q, phi = (p-1)*(q-1), e = 3, d = modInverse(e,phi); // Public key: [e,n], Private key: [d,n] return [[e,n], [d,n]] } RSAKeyPair(5,11) for (let i in RSAKeyPair(5,11)){ var encrypted_message; const encryption=c=>{ var m=2,e=c[0],n=c[1],Encrypted_Message=m**e%n console.log("Encryption: "+Encrypted_Message) encrypted_message=Encrypted_Message } const decryption=c=>{ var d=c[0],n=c[1],Decrypted_Message=encrypted_message**d % n console.log("Decryption: "+Decrypted_Message) } i=="0"?encryption(RSAKeyPair(5,11)[0]):i=="1"?decryption(RSAKeyPair(5,11)[1]):false }

This encrypts 2, and the receiver can decrypt that back to 2. The prime numbers chosen are very large for the RSA algorithm any higher than 2, if you wan to try, change the value of 2 at line 70 to 3 and you get different result.

This is because the prime factorization of large numbers takes time to compute. Todayโ€™s standard is 4,096-bit prime product.

Alt Text

Figure 4-5. 24096

Figure 4-5 shows the largest possible value for a
4,096-bit number.

Summary (๐Ÿ“š)

Alt text
Various string functions are summarized in Table 4-1.

Function Usage
charAt(index) Accesses a single character at index
substring(startIndex, endIndex) Accesses part of string from startIndex to endIndex
x str1 > str2 Returns true if str1 is lexicographically bigger than str2
indexOf(str, startIndex) Index of the desired str starting at startIndex
str.split(delimiter) Breaks a string into an array with the specified delimiter
str.replace(original,new) Replaces original with new
Table 4-1. String Function Summary

In addition, a JavaScript Regex object can be used for string validation. Table 4-2 provides a summary:

Regex Pattern Usage
/\d+/ Any numeric characters
/^\d+$/ Only numeric characters
/^[0-9]*.[0-9]*[1-9]+$/ Float numeric characters
/[a-zA-Z0-9] / Only alphanumeric characters
Table 4-2. Regex Summary

Up Next๐Ÿ‘‰ Part 05: JavaScript Array ๐Ÿ”ฅ ๐Ÿ”ฅ (July 07, 2020)


Alt text

Posted on by:

edisonnpebojot profile

Edison Pebojot(๐Ÿ‘จโ€๐Ÿ’ป)

@edisonnpebojot

I started using computers and writing software around 2008 and 2009, I taught myself Programming. However, I wasn't a typical "nerd-klutz". ๐Ÿ˜…

Discussion

pic
Editor guide