...

View Full Version : Resolved Alert result from for loop (homework)



Jimbolgs
12-09-2011, 08:49 AM
Hello, this is a homework project which kind of works but isn't perfect. My tutor said the result must be an alert but I ended up with a document.write instead.

He said it was fine but I'd like to know how to make it alert the result if anyone can help.

FWIW I tried making the contents of the for loop into a variable without success.



<form action="javascript: displaymessage();" method="get" />

<p>Please enter a word:

<input id="user_word" name="user_wrd" type="text" />

<input type="submit" value="Submit" />

</p>

<script type="text/javascript">

function displaymessage() {

/*Set a variable for the user's word*/
var word_forward;

/*Get the user's word from the form above and put it into the new variable*/
word_forward = document.getElementById("user_word").value;

/*Write the user's word from the variable to the page*/
document.write('<p>The word is: ' + word_forward + '.</p>');

/*Calculate the length of the user's word*/
var word_length=word_forward.length;

/*Write the length of the user's word to the page*/
document.write('<p>The word length is: ' + word_length + ' characters.</p>');

/*Subtract 1 from word length variable to allow for character index numbers starting from zero*/
word_length2=(word_length-1);

/*Set a variable for the character index number*/
var char_num;

/*Write the first part of the result line*/
document.write('The word spelled backwards is: ');

/*Begin character index number countdown loop: loop start value / loop end value / increment*/
for(char_num=word_length2; char_num>=0; char_num--) {

/*Write word backwards using charAt*/
document.write(word_forward.charAt(char_num) + " ");

}

}

</script>

Philip M
12-09-2011, 09:05 AM
Make sure you declare your variable before the loop starts


/*Begin character index number countdown loop: loop start value / loop end value / increment*/

var backwards = "";
for(char_num=word_length2; char_num>=0; char_num--) {
/*Write word backwards using charAt*/
backwards += (word_forward.charAt(char_num) + " ");
document.write(word_forward.charAt(char_num) + " ");
}
alert (backwards);
}
</script>

Remember that document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes
loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


We are sorry that due to a typographical error we referred to Wantage MP Ed Vaisey as 'one of the top sexist MPs'. This should have read 'one of the top sexiest MPs'. Wallingford Herald.

Jimbolgs
12-09-2011, 09:51 AM
Thanks for that, but a couple of questions...

1) What's the difference between:

var backwards = ""; and var backwards;

Is it simply that in the first instance you're declaring a variable and giving a null value and the second instance declaring an empty variable?

2) What is the += for in this line:

backwards += (word_forward.charAt(char_num) + " ");

Thanks again.

Old Pedant
12-09-2011, 09:57 AM
"" is *NOT* a null value. It *IS* a string. It's a string of zero length, but that's a hugely different thing than a null value.

+= is just shorthand.

a += b is shorthand for a = a + b

Philip M
12-09-2011, 03:05 PM
Thanks for that, but a couple of questions...

1) What's the difference between:

var backwards = ""; and var backwards;

Is it simply that in the first instance you're declaring a variable and giving a null value and the second instance declaring an empty variable?




var backwards = ""; defines a variable whose value is an empty string;

var backwards; simply declares a variable whose value is undefined

In the first case backwards += "Philip" results in the string value Philip.
In the second case the result is undefinedPhilip.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum