Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    curious behavior caused by white space in string

    I have encountered a strange problem,
    Code:
    document.getElementById("insertWord").innerHTML="please translate the word "+toTranslate[i]+"to french ";
    Does anyone know why when I put a space at the beginning of the string " to french" i does not change but when i remove the space everything works as expected.
    here is the rest of my code.
    Code:
    var score=0;
    var i =0;
    var toTranslate=new Array("hello","car","apple","dog","fish");
    var reponse=new Array("bonjour","voiture","pomme","chien","poisson");
    
    
    function questions() {
    var str =document.getElementById("inputbox").value;
    var trimmed = str.replace(/^\s+|\s+$/g, '') ;
    var trimmedl = trimmed.toLowerCase();
    	if (trimmedl ==reponse[i]) {
    		alert(trimmedl+" is correct");
    		score++;
    	}else {
    		alert("i am sorry to say that is incorect");
    	}
    	i = Math.floor(Math.random() * (1 - 0+ 1)) + 0;
    	document.getElementById("score").innerHTML="your score is "+score;
    	document.getElementById("insertWord").innerHTML="please translate the word "+toTranslate[i]+"to french yay";
    }
    function win() {
    	alert(score);
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by stef. View Post
    I have encountered a strange problem,
    Code:
    document.getElementById("insertWord").innerHTML="please translate the word "+toTranslate[i]+"to french ";
    Does anyone know why when I put a space at the beginning of the string " to french" i does not change but when i remove the space everything works as expected.
    Does it? It does not do that for me. Nor ought that to make any difference.

    Code:
    <div id = "insertWord"></div>
    
    <script type = "text/javascript">
    var i = 0;
    var toTranslate=new Array("hello","car","apple","dog","fish");
    document.getElementById("insertWord").innerHTML="please translate the word "+toTranslate[i]+" to french ";
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •