469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Javascript program to prompt and accept input from user

8
I'm having huge difficulties producing a script for this:
Write a javascript programme that will prompt for, and accept from the user, an input string which contains at least 8 characters. It should then prompt for and accept a numerical value that is no greater than the length of the input string and should output a version of the input string which takes the form of a string of the same length as the input string but consisting entirely of the letter which occurs in the input string at the position specified by the input number.

Been trying for days and just can't get it!!! :(((
Mar 9 '06 #1
8 11876
Banfa
9,064 Expert Mod 8TB
If you post the code of what you have so far we will try to help you, but since this sounds to me like a homework question I'm not just doing it for you.

BTW this would be better off in the Programming or Web Design forum.
Mar 9 '06 #2
lisaj
8
If you post the code of what you have so far we will try to help you, but since this sounds to me like a homework question I'm not just doing it for you.

BTW this would be better off in the Programming or Web Design forum.
<HTML>
<HEAD>
<TITLE>
Programme to accept no less than 8 characters
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">

function getStringInput(){

myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
if(!myString){
return
}

if (myString.length < 8)
{
alert("Please re-enter a Word with more than 8 Characters !") // tempory alert
getStringInput()
return
}

alert("Now enter a number that is smaller than the amount of letters in your inputed word" ) // tempory alert

getNumber()
}
function getNumber()
{
myNumber = window.prompt('Enter Your Number !','');
if(!myNumber){
return
}

if (count.myNumber > mystring.length)

window.prompt('The Number must be smaller than you inputed letters in Your word');
showResults()
}

function showResults(){

}

</script>

</HEAD>
<BODY onload="getStringInput()">

<div id="display"></div>

</BODY>
</HTML>
This is what i've done so far.....
Mar 9 '06 #3
lisaj
8
I'm having huge difficulties producing a script for this:
Write a javascript programme that will prompt for, and accept from the user, an input string which contains at least 8 characters. It should then prompt for and accept a numerical value that is no greater than the length of the input string and should output a version of the input string which takes the form of a string of the same length as the input string but consisting entirely of the letter which occurs in the input string at the position specified by the input number.

Been trying for days and just can't get it!!! ((

this is what i have so far.....
[HTML]<HTML>
<HEAD>
<TITLE>
Programme to accept no less than 8 characters
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">

function getStringInput(){

myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
if(!myString){
return
}

if (myString.length < 8)
{
alert("Please re-enter a Word with more than 8 Characters !") // tempory alert
getStringInput()
return
}

alert("Now enter a number that is smaller than the amount of letters in your inputed word" ) // tempory alert

getNumber()
}
function getNumber()
{
myNumber = window.prompt('Enter Your Number !','');
if(!myNumber){
return
}

if (count.myNumber > mystring.length)

window.prompt('The Number must be smaller than you inputed letters in Your word');
showResults()
}

function showResults(){

}

</script>

</HEAD>
<BODY onload="getStringInput()">

<div id="display"></div>

</BODY>
</HTML>[/HTML]
Mar 9 '06 #4
lisaj
8
<HTML>
<HEAD>
<TITLE>Program_TMA03
</TITLE>
<SCRIPT >

/* Program to read in a known number of data items and store them in an array */

var attendArray = new Array (5);
var dayArray = ['Monday','Tuesday','Wednesday','Thursday','Friday']


document.write('Array program to read in a known number of data items');
for (var day = 0; day < attendArray.length; day = day + 1)
{
attendArray[day] = window.prompt('Enter attendance value for ' + (dayArray + 1),'')
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of data input' + '<BR>' + '<BR>');

for (var day = 0; day < attendArray.length; day = day + 1)
{
document.write(dayArray[day] + ' : ' + attendArray[day] + '<BR>')
}

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

When I execute this programme I need the value button to have the day on;
eg; Enter the attendace for Monday
Next: Enter the attendance for Tuesday

I can only seem to get it to write up all of the days of the week at once. Can anyone tell me why?

Hopefully waiting....
Mar 9 '06 #5
Banfa
9,064 Expert Mod 8TB
OK the problem you have is the order that you have declared your functions. In Javascript you need to have declared a function before you try to call it. You declare the functions in the order

getStringInput
getNumber
showResults

but getStringInput calls getNumber and getNumber calls showResults. You need to delare them in the order

showResults
getNumber
getStringInput

This is the sort of feature you often get in an interpreted (as opposed to compiled) language.

On the contents of the function getStringInput, the recursive calling works but in this case since it could be replaced with a simple do ... while loop. In pseudo code this would look something like

Expand|Select|Wrap|Line Numbers
  1.   do
  2.   {
  3.     string = GetInputFromUser;
  4.  
  5.     if ( string.length <= 8 )
  6.     {
  7.       Display Error Message;
  8.     }
  9.   } while( string.length <= 8 );
  10.  
Just in case you are wonder psudo code is code that shows the structure of what needs to be done without neccessaryily being runnable code of a given language.

1 final note, the question calls for a string of at least 8 characters and the logic you have code tests for a string of at least 8 characters but your error messages says

"... more than 8 Characters ..."
Mar 9 '06 #6
Banfa
9,064 Expert Mod 8TB
When you say the value button I assume you mean window prompt box.

attendArray[day] = window.prompt('Enter attendance value for ' + (dayArray + 1),'')

should be

attendArray[day] = window.prompt('Enter attendance value for ' + dayArray[day]);

Make sure you understand why.
Mar 9 '06 #7
lisaj
8
When you say the value button I assume you mean window prompt box.

attendArray[day] = window.prompt('Enter attendance value for ' + (dayArray + 1),'')

should be

attendArray[day] = window.prompt('Enter attendance value for ' + dayArray[day]);

Make sure you understand why.
You are an absolute star ***** many thanks x
Mar 9 '06 #8
m3rajk
8
i really dislike doing other people's homework. but since you have tried, i will give you this clue:
you need 4 variables:
var input_string
var output_string
var is_length
var is_place

you should use onChange() to update your length

in psuedo-code:

Expand|Select|Wrap|Line Numbers
  1. <html start stuff>
  2. <script language="javascript">
  3.  
  4. // declare variables
  5.  
  6. function valid(){
  7.   if (length<8){
  8.     alert("you must have 8 or more characters");
  9.     return FALSE;
  10.   }else{
  11.     alert("thank you for entering 8 or more characters");
  12.     return TRUE;
  13.   }
  14. }
  15.  
  16. function getPlace(){
  17.   while(place is not between 0 and length){
  18.     place=window.prompt("enter a number 0 to length");
  19.   }
  20. }
  21.  
  22.  
that shouyld be a good starter
Mar 9 '06 #9

Post your reply

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

Similar topics

5 posts views Thread by TrvlOrm | last post: by
2 posts views Thread by atreide_son | last post: by
3 posts views Thread by J.P. Cummins | last post: by
reply views Thread by joestevens232 | last post: by
2 posts views Thread by steve | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.