Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-25-2012, 05:46 PM   PM User | #1
stef.
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
stef. is an unknown quantity at this point
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);
}
stef. is offline   Reply With Quote
Old 12-25-2012, 08:21 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,100
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:47 AM.


Advertisement
Log in to turn off these ads.