-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.js
176 lines (132 loc) · 4.65 KB
/
algorithms.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
function nameSwap()
{
// EXAMPLE
// 'Doe Jane' -> 'Jane Doe'
// 'Nowak Jan' -> 'Jan Nowak'
var nameSwapString = document.getElementById("nameSwap").value;
/*
//option 1 :
var stringToArray = nameSwapString.split(' ');
var reverseArray = stringToArray.reverse();
var reverseString = reverseArray.join(' ');
document.getElementById("swapNameResult").innerHTML = reverseString;
*/
//option 2 : Indexes
/*
var array = nameSwapString.split(' ');
var result = array[1] + ' ' + array[0];
document.getElementById("swapNameResult").innerHTML = result;
*/
//option 3 : One line
var nameSwapResult = nameSwapString.split(' ').reverse().join(' ');
document.getElementById("swapNameResult").innerHTML = nameSwapResult;
}
function palindrome()
{
//EXAMPLE PALIDROMES:
//1. 'Never odd or even'
//2. 'Do geese see God?'
var palindromeString = document.getElementById("palindrome").value;
var palindromeRemovedNonWordsCharacters = palindromeString.replace(/\W/g, '');
var caseInsensitiveString = palindromeRemovedNonWordsCharacters.toLowerCase();
var reversedString = caseInsensitiveString.split('').reverse().join('');
var palindromeResult = caseInsensitiveString === reversedString;
document.getElementById("palindromeResult").innerHTML = palindromeResult;
}
function reverseString()
{
//EXAMPLE:
//1. cat -> tac
//2. rain -> niar
var reverseString = document.getElementById("reverseString").value;
var reverseStringResult = reverseString.split('').reverse().join('');
//option 2:
//var reverseStringResult = [...reverseString].reverse().join('');
//option 3 - for loop:
/*
var reverseStringResult = '';
for (var i = reverseString.length-1; i >= 0 ; i--)
{
reverseStringResult += reverseString;
}
*/
document.getElementById("reverseStringResult").innerHTML = reverseStringResult;
}
function repeatString()
{
//EXAMPLE
//'cat', 5 -> 'catcatcatcatcat'
//'rain, 4' -> 'rainrainrainrain'
var repeatStringString = document.getElementById("repeatStringString").value;
var repeatStringNumber = document.getElementById("repeatStringNumber").value;
var finalString = '';
if(repeatStringNumber <= 0)
{
finalString = ' Error. Incorrect data. ';
}
//for loop
for (let index = 1; index <=repeatStringNumber; index++)
{
finalString += repeatStringString;
}
//option 2 : while loop
/*
while (num > 0)
{
finalString += str;
num--;
}
*/
document.getElementById("repeatStringResult").innerHTML = finalString;
}
function alphabetizeString()
{
//EXAMPLE
//cat -> act
//rain -> ainr
var alphabetizeStr = document.getElementById("alphabetizeString").value;
var alphabetizeStrResult = alphabetizeStr.split('').sort().join('');
//option 2
//var alphabetizeStrResult = [...alphabetizeStr].sort().join('');
document.getElementById("alphabetizeStringResult").innerHTML = alphabetizeStrResult;
}
function findTheLongestString()
{
//EXAMPLE
//Hello, what's up? -> Hello
//I like trains :3 -> trains
var findTheLongestStringStr = document.getElementById("findTheLongestStringInput").value;
var noSpecialCharacters = findTheLongestStringStr.replace(/\W/g,' ');
var longestWord = '';
var wordArray = noSpecialCharacters.split(' ');
for(let i=0; i<wordArray.length; i++ )
{
if(wordArray[i].length > longestWord.length)
{
longestWord = wordArray[i];
}
}
document.getElementById("findTheLongestStringResult").innerHTML = longestWord;
}
function findTheLargestElement()
{
//EXAMPLE
//1. 1,3,10,4,6,7,8 -> 10
//2. 302,17,35,66,100,1 -> 302
var findTheLargestElementData = document.getElementById("findTheLargestElementInput").value;
var largestElement = null;
var findTheLargestElementSplit = findTheLargestElementData.split(',');
largestElement = Math.max.apply(null,findTheLargestElementSplit);
document.getElementById("findTheLargestElementResult").innerHTML = largestElement;
}
function findTheSmallestElement()
{
//EXAMPLE
//1. 1,3,10,4,6,7,8 -> 1
//2. 302,17,35,66,100,1 -> 1
var findTheSmallestElementData = document.getElementById("findTheSmallestElementInput").value;
var smallestElement = null;
var findTheSmallestElementSplit = findTheSmallestElementData.split(',');
smallestElement = Math.min.apply(null,findTheSmallestElementSplit);
document.getElementById("findTheSmallestElementResult").innerHTML = smallestElement;
}