188
rated 0 times
[
191]
[
3]
/ answers: 1 / hits: 26699
/ 9 Years ago, thu, july 23, 2015, 12:00:00
I have this event...
<textarea id=chat> </textarea>
<button type=button onclick=play_song();>talk</button>
... triggering the following function
:
var input = function() {
var chat = document.getElementById(chat).value.split( );
return chat && console.log(chat);
}
then there's this function
:
function setIntersection(a, b) {
var result = [];
for (var i = 0; i < a.length; i++) {
if (b.indexOf(a[i]) !== -1 && result.indexOf(a[i]) === -1) {
result.push(a[i]);
}
}
return result;
}
a prototype function
:
Song.prototype.lyricsIntersect = function(input) {
var bestSong = null;
var bestCount = -Infinity;
for (var i in songs) {
var currentCount = setIntersection(songs[i].lyrics, input).length;
if (currentCount > bestCount) {
bestSong = songs[i];
bestCount = currentCount;
}
}
return bestSong && bestSong.name;
}
the code ends here:
function play_song() {
var id = Song.prototype.lyricsIntersect(input);
var element = document.getElementById(id);
element.play();
}
but console.log
returns: Uncaught TypeError: b.indexOf is not a function
if I test var input = [one, two];
, however, I get the intersection done on the code depending on input
.
what am I missing?
More From » javascript