473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 12263
Banfa
9,065 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,065 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,065 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

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

Similar topics

5
by: TrvlOrm | last post by:
Can any one please help me...I am new to JavaScript and I have been struggling with this code for days now and can't figure it out. I would like to get the Buttons to correspond with the action...
2
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
3
by: J.P. Cummins | last post by:
In my ASP.NET application, I wish to have a page for administrators to edit items in a list. Preferably, I would like to use the javascript prompt for the 'rename' function, and a javascript alert...
0
by: joestevens232 | last post by:
I am seriously stuck and have been working on this for hours and hours and can't figure out my next step....heres the program assignment. This vending machine dispenses 1. M&Ms ($.65), 2. Chips...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
6
by: ahin | last post by:
can any one help or give an idea about writing or from where to strat the following program? • Load a list of available courses from a text file into an array (max 4 courses). File format can...
2
by: steve | last post by:
Hi guys, I have a problem with the Safari browser. specifically the following javascript will not print any headings until after the JS has executed. <script type = "text/javascript">
6
by: aureao4 | last post by:
I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.