javascript - Eloquent JS Exercise (Ch.18 Q. 2) -


i hoping can me figure out i'm doing wrong... there's exercise in eloquent js book asks write code can suggest words/values users type... code i've written below. happens when run code, div element's text content changes wrong value. specifically, it's set string of elements inside array 'terms'. cant figure out why happens!

    <input type="text" id="field"> <div id="suggestions" style="cursor: pointer"></div>  <script>   // builds array global variable names,   // 'alert', 'document', , 'scrollto'   var terms = [];   (var name in window)     terms.push(name);   var input = document.queryselector('input');   var div = document.queryselector('#suggestions');  input.addeventlistener("input", function(event){   var last = input.value.lastindexof(" ")+1;   var check = input.value.slice(last);   var reg = new regexp(check);   (var i=0; i<terms.length; i++) {     if (reg.test(terms[i])) {       var text = document.createtextnode(terms[i]);       div.appendchild(text)};   }; })  </script> 

i guess forgot clean div before each change in input. added space after each word make output more readable.

// builds array global variable names,  // 'alert', 'document', , 'scrollto'  var terms = [];  (var name in window)      terms.push(name);  var input = document.queryselector('input');  var div = document.queryselector('#suggestions');    input.addeventlistener("input", function(event){    div.innerhtml = '';    var last = input.value.lastindexof(" ")+1;    var check = input.value.slice(last);    var reg = new regexp(check);    (var i=0; i<terms.length; i++) {      if (reg.test(terms[i])) {        var text = document.createtextnode(terms[i] + ' ');        div.appendchild(text)};    };  })
<input type="text" id="field">  <div id="suggestions" style="cursor: pointer"></div>

with code, display name of properties window object contains last word input. try writting "window location document". looking for?


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -