-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest.js
28 lines (23 loc) · 887 Bytes
/
longest.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
function longestWord(string) {
var array = string.split(' ');
var long = '' // turns string into an array
for(var i = 0; i < array.length-1; i++) {// goes through array words
if(array[i].length < array[i + 1].length) { // compares two words
long = array[i]; // sets the first word to longer string in comparison
} else {
long = array[i+1]; // else make the second word the longest, then back through the loop
}
}
return long; // here we return our longest word*/
}
var typical = "asdfkljasdfkj tree dog house family sky shoes horizon longerrrrr sun";
console.log(longestWord(typical));
/*function longestWord(string) {
var array = string.split(' ');
var long = ''
for(var i = 0; i < array.length-1; i++) {
array[i].length > array[i + 1].length ? long = array[i] : long = array[i+1];
}
return long;
}
console.log(longestWord(typical));*/