javascript - Local variables giving global trouble -


i'm js super n00b.

i asked aspect of problem in post (puzzling behavior if ( ) statement) on if statements looks actual issue related scope of variable i've created. seems after declaring (what think is) global variable, other functions in code cannot access variable.

i'm doing js project/program prompts user input word , program reverses word input.

in previous post (pp) user correctly determined getting 'false' console message (see code) no matter length of word input because assigning value variable when page loads not reading again when user clicks button on page.

if variable 'word' local i'm able 'false' console message , when variable 'word' global i'm able 'referenceerror.'

any ideas has appreciated.

see js code below:

var word = document.getelementbyid('wordchoice').value;   var lttrs = [];  function flipfail () {     alert("please enter word of @ least 2 characters.");     console.log(false);      var inputerrarr = ['has-error', 'has-feedback'];     var inputerrfdbk = ['glyphicon', 'glyphicon-remove'];     wordchoice.style.backgroundcolor = "#ffdbaa";      (var = 0; < inputerrarr.length; ++) {         addclass(wordinput, inputerrarr[i]);     }      (var = 0; < inputerrfdbk.length; ++) {         addclass(glyph, inputerrfdbk[i]);     }      document.getelementbyid('wordchoice').value = " ";  }   // end flipfail()   function flipsuccess (){      (var = 0; < word.length; ++) {         lttrs.push(word.charat(i));     }      lttrs.reverse();     var reversedword = lttrs.join('')     alert("your reversed word is: " + reversedword);     console.log(true);      document.getelementbyid("flip").innerhtml = "flip again!";     document.getelementbyid('wordchoice').value = " ";  } // en flipsuccess ()   function flipchk () {       if (word.length < 2) {         flipfail ();      } else {          flipsuccess ();     } } 

see implemented code here: http://supsean.com/supsean/flipr/flipr.html

you need set word in flipchk(). you're setting when page first loaded, before user has entered form, not when user clicks on flip button.

then, instead of using global variable, pass argument function. in general, avoid using global variables unless have to.

function flipchk () {     var word = document.getelementbyid('wordchoice').value;     if (word.length < 2) {         flipfail ();      } else {          flipsuccess (word);     } }  function flipsuccess (word){     var lttrs = [];     (var = 0; < word.length; ++) {         lttrs.push(word.charat(i));     }      lttrs.reverse();     var reversedword = lttrs.join('')     alert("your reversed word is: " + reversedword);     console.log(true);      document.getelementbyid("flip").innerhtml = "flip again!";     document.getelementbyid('wordchoice').value = " ";  } // en flipsuccess ()  

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -