-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_findDifferentWords_from_2_strings.html
More file actions
54 lines (43 loc) · 1.37 KB
/
Copy pathstrings_findDifferentWords_from_2_strings.html
File metadata and controls
54 lines (43 loc) · 1.37 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
/*
* Given two strings, we need to find only different words. Words which don't intersect (don't appear in both strings).
*/
function findDifferentWords(s, t) {
// making an array of words from "s" and "t"
var sArray = sentenceToArrayConverter(s);
var tArray = sentenceToArrayConverter(t);
var findDifferentWords = [];
for (var i=0; i< sArray.length; i++){
if( tArray.indexOf(sArray[i]) == -1 ) {
findDifferentWords.push(sArray[i]);
}
}
return findDifferentWords
}
// array of words maker
function sentenceToArrayConverter(string){
var arr = [],
w = "";
for(var i=0; i < string.length; i++){
// this is not a whitespace
if( string[i].charCodeAt(0) != 32) {
w = w + string[i];
}
if ( (string[i].charCodeAt(0) == 32 || i == string.length-1) && w.length != 0 ){
arr.push(w);
w = "";
}
}
return arr
}
console.log( findDifferentWords(" This is a sentence with words", "This sentence") );
</script>
</body>
</html>