Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
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

 Answers
23
var currentCount = setIntersection(songs[i].lyrics, input).length;


should be



var currentCount = setIntersection(songs[i].lyrics, input()).length;


this also relies on input being corrected as follows



var input = function() {
var chat = document.getElementById(chat).value.split( );
return chat;
}

[#65697] Wednesday, July 22, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
luna questions
;