By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

Determine position of letter by inserting number, output letter

P: 2

Please could someone shed some light on this as I am really struggling to understand how to do this.

Given a word entered into a window.prompt EG "forum", how do I create a script to determine the postion of a letter by entering a number no longer than the string and output that letter to screen.

I have completed the prompting and selection the bold section below is where I become stuck. Any help would be appreciated greatly.

prompt - enter a word no more than 5 characters

user submit "forum"

prompt - select a number from 1 to 5

user submit "2"

write - your chosen letter is o


Jun 5 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,208
this was an easy one so i wrote a pretty good example up for you. please respond with anything you don't understand in this script and i will explain it further.
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. window.onload = getLetter();
  3. function getLetter() {
  4. var resp = "";
  5. resp = window.prompt("type in a 5 letter word", "");
  6. if (resp.length!=5) { alert("You did not type in a 5 letter word!"); getLetter(); return false; }
  7. var num = 0;
  8. num = window.prompt("Select a number 1-5", "");
  9. num = parseFloat(num);
  10. if (num>0 && num<6) { document.write("Your chosen letter is " + resp.charAt(num-1)); } else { alert("That is not a number 1-5"); getLetter(); return false; }
  11. return true;
  12. }
  13. </script>
Jun 5 '07 #2

P: 2
Hi Thanks iam_clint,

Greatly appreciate your help.

Strange how once you see something work you realise how it should have been done.

Once again thanks.

Jun 6 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.