forked from alexf1225/coderbyte_js_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcb_longestWord.js
146 lines (111 loc) · 3.32 KB
/
cb_longestWord.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
//Using the JavaScript language, have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.
function LongestWord(sen) {
//filter out nonsense characters with regex, turn sentence into an array of words
var arr = sen.match(/[a-zA-Z ]/g).join("").split(" ");
//find longest word
var longestWord = arr[0]
for ( var i = 0; i < arr.length - 1; i++){
if (arr[i+1].length > longestWord.length){
longestWord = arr[i+1];
}
}
return longestWord;
}
//My Test
var sen = "###ace %%%%%JOIN abracadabra";
console.log(LongestWord(sen)); //solution: "abracadabra"
/*Solutionn Not using RegEx
var isAlphabet = function(testChar){
var alphabetText = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
alphaArr = alphabetText.split(""),
result = false;
for (var i in alphaArr){
//look for a match, then return true
if (testChar === alphaArr[i]){
result = true;
}
}
return result;
};
var remStrangeText = function (str){
var arr = str.split(""),
result = "";
for (var i in arr){
if (isAlphabet(arr[i])){
result += arr[i];
}
}
return result;
};
function LongestWord(sen) {
// code goes here
// split the sentence into an array of words
var arr = sen.split(" ");
//clean up each array element of strangetext
for (i in arr){
arr[i] = remStrangeText(arr[i])
}
//find the longest word
var longestWord = arr[0];
for (var i in arr){
if (arr[i].length > longestWord.length){
longestWord = arr[i];
}
}
sen = longestWord
return sen;
}
*/
/* shortest solution that can be entered in coderbyte
function LongestWord(sen) {
// code goes here
//remove weird stuff
var arr = sen.match(/[a-zA-Z ]/g).join("").split(" ");
//find longest word
var longestWord = arr[0]
for ( var i = 0; i < arr.length - 1; i++){
if (arr[i+1].length > longestWord.length){
longestWord = arr[i+1];
}
}
return longestWord;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(LongestWord(readline()));
*/
/* First Solution that can be entered in Coderbyte
function LongestWord(sen) {
//create a filter
var isLetter = function(testChar){
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
var arr = alpha.split("");
for (var i in arr){
if (testChar === arr[i]){
return true;
}
}
return false;
};
//filter out non alphabet
var arr = sen.split("");
var newSen = '';//result of filtering
for(var i in arr){
if (isLetter(arr[i])){
newSen += arr[i];
}
}
//split the words and look for longest
newSen = newSen.split(" ");
longestWord = newSen[0];
for (var i in newSen){
if (newSen[i].length > longestWord.length){
longestWord = newSen[i];
}
}
return longestWord;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
print(LongestWord(readline()));
*/