View Full Version : Needing help with a small program???
MemmnocK
01-30-2006, 12:27 PM
Hi guys, I have been set a task at college for to do the following below, i am just starting javascript now and getting lost at some very simple bits, I would be gratefull if anyone can show me the code for this so i can see where im going wrong in my head.?
-------------------------------------------------------------------------------------
Write a JavaScript program 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 from the user, 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.
For example, if the user inputs the string abracadabra followed by the
number 7 then the output should be the string ddddddddddd
If the user fails to input a sufficiently long string then they should be
repeatedly prompted to re-enter a string of an acceptable length. If the
user then fails to enter a number that falls within the length of the
entered string they should be repeatedly asked to re-enter a number
that falls in the acceptable range.
You should include in your TMA solution document a copy of your
JavaScript program code and also a screen shot of your working
program when the user inputs an acceptable length string followed by a
consistent numerical value.
-------------------------------------------------------------------------------------
Here is my feeble atempt so far :(
<HTML>
<HEAD>
<TITLE> Programme to accept no less than 8 characters
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
var myString;
myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
"Is it LENGTH I use here or a WHILE LOOP to keep prompting for a word if the word has less than 8 characters?"
i.e. while (myString < 8 )
{
document.write('The name should contain more than 8 characters'+<BR>');
myString = window.prompt('Please re-enter again !');
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Memm.
Your assignment is done and sitting on my desktop.
It is in your interests to attempt to find the solutions so I am only post this bit for now
If the length of myString is less than 8 then alert the user and re-run the function
if(myString.length < 8 ){
alert("PLease enter a Word with more than 8 Characters")
getStringInput()
return
}
This is not the complete function, you can use the same theory for the second function ..... oops, shouldn't have said that.
Also you do not want to use document.write() at that stage of the script otherwise you will not be able to proceed you are better using the alert.
MemmnocK
01-30-2006, 07:45 PM
Right this is what i undersatnd so far..:thumbsup:
1st of all.
var myString;
var number;
This is the variable that is in the "condition of the Bolean Expression" which if evaluates to true? i.e. The length of the word is less than 8 characters needs to be input again until it evaluates to false.
2nd - I need to get a window prompt box to input the characters into.
myString = window.prompt('Please enter a word with more than 8 Characters','');
if(myString.length < 8 )
{
alert("PLease enter a Word with more than 8 Characters")
getStringInput()
return
}
{
number = window.prompt('Please enter a number no bigger than your Word','');
????? Now im lost lol a bit
This isnt correct from here i think as it doesnt run? :confused:
Ok, I'll give you a bit more.
There will be 3 functions.
The first function will get the string
The second function will get the number
The third function will display the results
Here's the first
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
function getStringInput(){
myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
if(!myString){
return
}
if(myString.length < 8){
alert("Please enter a Word of 8 or more Characters")
getStringInput()
return
}
alert("The string entered is\n\n"+myString+"\n\nThe second function will now run, when you have figured it out" ) // tempory alert
getNumberInput()
}
function getNumberInput(){
showResults()
}
function showResults(){
}
</script>
</HEAD>
<BODY onload="getStringInput()">
<div id="display"></div>
</BODY>
</HTML>
MemmnocK
01-30-2006, 10:52 PM
Thanx very much friend i see now where i am wrong as i can actuallt see the beginning of the program working.!!
I have changed the part where you had:-
if (myString.length < 8) to while (myString.length < 8)
as the alert box was only repeating twice with if so tried while so it could go on infinitly if 8 were not added !
I will try to add the next part as i can see the layout you have approached with, but many thanx for this piece of the code :thumbsup: as its really helped me. As you say its pointless giving the full code without trying as thats not learning? I will add the other pieces next and see if they work.
Memm.:thumbsup:
Pyth007
01-31-2006, 01:29 PM
One of the major ideas behind procedural / object-oriented programming is that each function does only one small piece of the entire procedure without having any knowledge about what the rest of the program is doing. Thus instead of having each function call the next one in line, you may want to have a fourth function that controls the process-flow:
function processFlow()
{
getString();
getNumber();
showResults();
}
There should be no need to change the if to while
I re-tried the code I posted a dozen times always inputting less than 8 characters and was alerted each time.
MemmnocK
01-31-2006, 04:37 PM
Hi Mr J..:o Sorry.:o
I must have been doing something wrong? "IF" does work the same as "WHILE", accept my appologies! as wasnt trying to be smart.!
I have done a bit more to the code and am now at the Window Prompt Box where i need to input a number smaller than the word letters inputed.!
But now im trying to see what i have to do next???
I know the program has to execute "if the number is less than the word letters" move onto showing that letter in the position of the word at what number position a letter is at and show that letter the number value of times..:confused: I think? If not then ask again for a number less than the letters inputed!
Here is what i have added to your code so far.? Highlighted in red.!!!
--------------------------------------------------------------------------
<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)
{
window.prompt("Please re-enter a Word with more than 8 Characters !")
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>
--------------------------------------------------------------------------
Can you let me know what is needing to happen in the code cycle to execute, but not the code just now ! as I think im getting lost in whats happening and whats needing to come next in the structure of it???
Memm. :thumbsup:
As I did in the first function you need to assign the variable, myNumber, the value retrieved from the prompt. You will also need to include in the prompt message myString.length which was derived from function 1 to tell the user the range of numbers they can input.
1 to myString.length
Stop the function should the user press the cancel button or the ok button having not entered a value.
Compare the number entered into the number prompt against the length of the string entered in the first prompt.
If incorrect stop the function and run again.
If correct then go to the next function.
For the third function you will have to familiarise yourself with the charAt() method
Just noticed in the code you have posted that in the first function you have put a second prompt in the conditional statement
if (myString.length < 8){
window.prompt("Please re-enter a Word with more than 8 Characters !")
getStringInput()
return
}
this should be an alert otherwise the prompt will be shown twice and confuse the user
MemmnocK
02-02-2006, 12:22 PM
Im really really lost now :(
I can seem to see it clearly enough but know what has to happen?
am i right here where the 2nd function is comparing the number inputed against the length of the string inputed??
}
getNumber()
}
function getNumber(){
myNumber = window.prompt('Now enter a number smaller than the amount of letters in your word !','');
if(!myNumber){
return
}
if (myNumber.count > mystring.length)
{
alert('The Number must be smaller than you inputed letters in Your word');
getNumberInput()
return
as this isnt returning an alert box if the number is more than the inputed characters.? am i doing it wrong somewhere.?
Memm.:confused:
You are so close.
Where you have gone wrong is with this
if (myNumber.count > mystring.length)
You do not need the .count, the variable myNumber will be assign the prompt value
Here's what you should have, notice that I have included || myNumber==0 incase someone inputs a zero
function getNumberInput(){
myNumber = window.prompt('Now enter a number smaller than the amount of letters in your word !','');
if(!myNumber){
return
}
if (myNumber > myString.length || myNumber==0){
alert('The Number must be smaller than you inputed letters in Your word');
getNumberInput()
return
}
showResults()
}
For the third and final function, showResults(), you will need to be familiar with the charAt() method as this is going to get the character at the position stated by the value of myNumber.
1) Get the character at position myNumber
2) Loop myNumber times and display the character for each iteration of the loop
MemmnocK
02-02-2006, 04:24 PM
Hi J, :thumbsup:
I can see what you have done here ! and why? :)
But when i link the 1st part with the second part they dont work? well the 1st part works so that if i input a word with less than 8 characters it gives the alert box but in the 2nd part it cant get it to show the alert box???
Am i not joining them properly or missing a semi ; or something??
Im reading up on charAt and index just now so will try the 3rd part after i get this 2nd part to show an alert box..
Here is the 2 parts joined together as i have them !!
------------------------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
function getStringInput(){
myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
if(!myString)
return
if(myString.length < 8)
{
alert("Please enter a Word of 8 or more Characters")
getStringInput()
return
}
getNumber()
}
function getNumber(){
myNumber = window.prompt('Now enter a number smaller than the amount of letters in your word !','');
if(!myNumber){
return
}
if (myNumber > mystring.length || myNumber==0)
// || myNumber==0 is here incase someone inputs a zero!
{
alert('The Number must be smaller than you inputed letters in Your word');
getNumberInput()
return
}
showResults()
}
function showResults(){
}
</script>
</HEAD>
<BODY onload="getStringInput()">
</BODY>
</HTML>
----------------------------------------------------------------------------------------------
Memm. Thanx for you help..:)
Change the 2 instances of getNumber() to
getNumberInput()
MemmnocK
02-02-2006, 05:48 PM
Im sorry to be a nuisance J :o
but done that and i still cant get an alert box for the inputing a larger number than the letters inputed.??:confused:
Memm.
Here you go
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
function getStringInput(){
myString = window.prompt('Enter a Word with more than 8 Characters Please!','');
if(!myString)
return
if(myString.length < 8)
{
alert("Please enter a Word of 8 or more Characters")
getStringInput()
return
}
getNumberInput()
}
function getNumberInput(){
myNumber = window.prompt('Now enter a number smaller than the amount of letters in your word !','');
if(!myNumber){
return
}
if (myNumber > myString.length || myNumber==0)
// || myNumber==0 is here incase someone inputs a zero!
{
alert('The Number must be smaller than you inputed letters in Your word');
getNumberInput()
return
}
showResults()
}
function showResults(){
}
</script>
</HEAD>
<BODY onload="getStringInput()">
</BODY>
</HTML>
The problem was with this line
if (myNumber > mystring.length || myNumber==0)
should be
if (myNumber > myString.length || myNumber==0)
Javascript is case sensitive
dumpfi
02-02-2006, 09:10 PM
Some errors I found in your script:
Checking if the given number is 0 is not sufficient, because negative values will pass it.
Your prompt message for getting the number is wrong, because it "excludes" the length of the given string from the list of valid numbers. However, your checks show the correct behavior and don't throw an alert in this case.
Keep in mind, that a number may also be a floating point value.
dumpfi
dumpfi
Checking if the given number is 0 is not sufficient, because negative values will pass it.
Keep in mind, that a number may also be a floating point value.
For this type of basic script there is no valid reason for anyone to input a negative number or a floating point number.
Your prompt message for getting the number is wrong, because it "excludes" the length of the given string from the list of valid numbers. However, your checks show the correct behavior and don't throw an alert in this case.
My original script does include the length of the string within the prompt message, the getNumberInput() function also checks that it is a number that has been entered.
If you read through MemmnocKs posts and I give the first as an example
I have been set a task at college for to do the following below, i am just starting javascript now and getting lost at some very simple bits,
you will see that he/she is struggling slightly with even the basics of this script, no offence intended to MemmnocK, or yourself, so I deem it best to keep this as simple as possible until MemmnocK completes the final function and has a better understanding of the script in its entirety.
Then is the best time to introduce any corrective statements
MemmnocK
02-03-2006, 10:47 AM
Thanx J :thumbsup:
I'll move onto the last part charAt and index today, i have been reading up so know what to do but how to do its a different thing lol.
Memm.
MemmnocK
02-03-2006, 04:02 PM
Hi J,
Ive had charAt() going through my head all day now and getting confused??:confused: lol
What im trying to get now i think?
1. The character at myNumberInput on my myStringInput word.? is it?
2. Then write the character the numberInput times of that character.? is it?
So i get a character times the amount of number inputed? i think:D
Do i need assign a variable for the character like myCharacter = myCharacter;
and then;
Do i use the "get" method to get the character? ie.
get (myCharacter.charAt(myStringInput + myNumberInput) ???:confused:
Memm.
Using the following as an example
If you input the word "computor" and the number 4 function 1 will assign "computor" to the variable myString and function 2 will assign the number 4 to variable myNumber
myString="computor"
myNumber="4"
We now need to get the 4th character in the word computor which as we count it would be the character "p" but, javascript counts from zero and would show the character "u"
0 = c
1 = o
2 = m
3 = p
4 = u
5 = t
6 = o
7 = r
so we want the value of myNumber minus one therefore we go
get the character in myString at position myNumber-1
The following is the first line of function 3 which for the above example would assign the character "p" to the variable getChar
getChar=myString.charAt(myNumber-1)
The next step is to create the loop to get the character the required number of times
MemmnocK
02-03-2006, 05:43 PM
Thanx J..:)
That example is very understandable indeed :thumbsup:
You have the Patience of a Saint..LOL
Memm.
MemmnocK
02-04-2006, 04:19 AM
Hi J, :confused: 4.17am on a friday night sat morn and still trying lol
I have this so far am i along the correct lines ?
}
showResults()
}
function showResults(){
//get the character in myString at position myNumber-1
getChar=myString.charAt(myNumber-1)
for (var count = 1; count = myNumber; count = myNumber + Char)
{
document.write(character)
}
I can see this javascript is gonna take some time to master..:rolleyes: ( SPIRITS HIGH)
EDIT..Its now 9.23am and 5 cans o cider later only have this..
}
showResults()
}
function showResults(){
var myString, myNumber, Char;
//get the character in myString at position myNumber-1
getChar=myString.charAt(myNumber-1)
for (var count = 1; count = myNumber; count = myNumber + Char)
{
document.write(character + myNumber)
}
Memm.
and still not working.I know and understand what to do and why its doing it but not how to write it down as code...Arggg.
If there was a prize for the hrs put in on this i would win i think..LOL (SPIRITS LOW)
Memm.
Ok, we now know what the character is because of this line
getChar=myString.charAt(myNumber-1)
and we know how many times we want to show that character through the value of myNumber therefore a loop is needed to iterate from zero to myNumber so the final function would be
function showResults(){
getChar=myString.charAt(myNumber-1) // get the character in myString at position myNumber-1
charactersToShow="" // declare as an empty string
for(var i=0;i<myString.length;i++){ // loop myString.length times
charactersToShow+=getChar // on each iteration of the loop concatenate the character
}
document.getElementById("display").innerHTML=charactersToShow // display the results
}
Here is the script in full
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
characterLimit=8
function getStringInput(){
myString = window.prompt('Enter a Word with '+characterLimit+' or more Characters','')
if(!myString){ // if no input
return
}
if(myString.length < characterLimit){ // if less than characterLimit
alert("Please enter a Word of "+characterLimit+" or more Characters") // alert the user
getStringInput() // and re-run the function
return
}
getNumberInput() // if ok run next function
}
function getNumberInput(){
myNumber = window.prompt('Enter a number between 1 and '+myString.length+'','')
if(!myNumber){ // if no input
return
}
if(isNaN(myNumber)){ // check to see if a valid number has been entered
alert("Please enter a number only between 1 and "+myString.length+"") // if not alert the user
getNumberInput() // and re-run the function
return
}
if(myNumber > myString.length || myNumber<=0){ // if the number is greater than the number of characters, zero, or less
alert("You must enter a number between 1 and "+myString.length) // alert the user
getNumberInput() // and re-run the function
return
}
showResults() // if ok run next function
}
function showResults(){
getChar=myString.charAt(myNumber-1) // get the character in myString at position myNumber-1
charactersToShow="" // declare as an empty string
for(var i=0;i<myString.length;i++){ // loop myString.length times
charactersToShow+=getChar+" " // on each iteration of the loop concatenate the character (space included for ease of reading)
}
document.getElementById("display").innerHTML=charactersToShow // display the results
summary()
}
function summary(){ // additional function just to confirm the prompt entries and the resulting character
document.getElementById("display2").innerHTML="You entered the characters <b>" +myString+ "</b><br><br>The character at position <b>" +myNumber+ "</b> is<b> " +getChar+"</b>"
}
</script>
</HEAD>
<BODY>
<center>
<button onclick="getStringInput()">Start</button>
<BR><BR>
<div id="display" style="font-size:20px"></div>
</center>
<BR><BR>
<div id="display2"></div>
</BODY>
</HTML>
See if you can pick out the additional enhancements.
Hope this has been helpful to you
Mr J
dumpfi
02-04-2006, 02:05 PM
There's a slight mistake in Mr. J's script. You must loop from 0 to myString.length - 1 instead of myNumber - 1.
dumpfi
MemmnocK
02-04-2006, 02:18 PM
I dont know how to thank you J..:o
As you showed allot of patience with me and helped me allot with seeing the way it should be layed out and done. :thumbsup:
You dont know the amount of hrs that ive put into this trying it BUT i have taken away with me allot of understanding on what does what and when it has to be done.!!!!!
I have looked through your full code and omitted some of it like the start button and where it says input a number between 1-whatever your number was?
So now i have the full thing and will work of that for the other question's and the format you done doing it. So Many Thanx and hope to speak soon..
Memm. :thumbsup: :thumbsup: :thumbsup:
dumpfi
Thank you, you are correct, looks like I did not read the question correctly but I think it only needs to be myString.length
Memmnock
Please note that I have amended my previous posts and you must change the line
for(var i=0;i<myNumber;i++){
to
for(var i=0;i<myString.length;i++){
Sorry for any confusion
MemmnocK
02-05-2006, 11:14 AM
Hi J..:)
I have ammended the program to work the way i like and when iput that new line in it dont work..:confused: :confused: :confused:
But with the old version with for(var i=0;i<myNumber;i++) it works perfect.:thumbsup:
It is correct i think as this "WORKS"
Here is what i have done to it..Can you see anything wrong with it here as it full fills all the criteria thats asked for :)
Please try this in your browser to see if it full fills the criteria for whats asked?
--------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Program to accept no less than 8 characters</TITLE>
<script type="text/javascript">
function getStringInput()
{
myString = window.prompt('Enter a Word with more than 8 Characters','')
if(!myString)
{
return
}
if(myString.length < 8)
{
alert("Please enter a Word of 8 or more Characters")
getStringInput()
return
}
getNumberInput()
}
function getNumberInput()
{
myNumber = window.prompt('Enter a number smaller than the amount of letters in your word','')
if(!myNumber)
{
return
}
if(myNumber > myString.length || myNumber<=0)
{
alert("You must enter a number smaller than the amount of letters in your word !!!")
getNumberInput()
return
}
showResults()
}
function showResults()
{
getChar=myString.charAt(myNumber-1)
charactersToShow=""
for(var i=0;i<myNumber;i++)
{
charactersToShow+=getChar+" "
}
document.getElementById("display").innerHTML=charactersToShow
summary()
}
function summary()
{
document.getElementById("display2").innerHTML= " "
}
</script>
</HEAD>
<BODY>
<center>
<button onclick="getStringInput()">Click here to Start Q-1-(ii) JavaScript Program !</button>
<div id="display" style="font-size:30px"></div>
</center>
</BODY>
</HTML>
--------------------------------------------------------------------------
Also J could you please show me what to EDIT if i didnt want the Start Button at the start of the program???. Just so i know as this is a very nice little start and hope to use it on every question as an added bonus to the code.
Memm.:thumbsup:
Here you go, you'd removed the display2 div which the summary function referenced.
I have removed the function and put in that new line.
All appears to work ok.
Give it a try and let me know how it goes.
<HTML>
<HEAD>
<TITLE>Program to accept no less than 8 characters</TITLE>
<script type="text/javascript">
function getStringInput(){
myString = window.prompt('Enter a Word with more than 8 Characters','')
if(!myString){
return
}
if(myString.length < 8){
alert("Please enter a Word of 8 or more Characters")
getStringInput()
return
}
getNumberInput()
}
function getNumberInput(){
myNumber = window.prompt('Enter a number smaller than the amount of letters in your word','')
if(!myNumber){
return
}
if(myNumber > myString.length || myNumber<=0){
alert("You must enter a number smaller than the amount of letters in your word !!!")
getNumberInput()
return
}
showResults()
}
function showResults(){
getChar=myString.charAt(myNumber-1)
charactersToShow=""
for(var i=0;i<myString.length;i++){
charactersToShow+=getChar+" "
}
document.getElementById("display").innerHTML=charactersToShow
}
</script>
</HEAD>
<BODY>
<center>
<button onclick="getStringInput()">Click here to Start Q-1-(ii) JavaScript Program !</button>
<div id="display" style="font-size:30px"></div>
</center>
</BODY>
</HTML>
If you do not want the button delete this line
<button onclick="getStringInput()">Click here to Start Q-1-(ii) JavaScript Program !</button>
but then you would have to put getStringInput() in the opening body tag so that the function runs when the page loads
<BODY onload="getStringInput()">
MemmnocK
02-05-2006, 04:38 PM
Hi J..
Ive just run your last program and its wrong as its returning the wrong amount of letters in the results.
Its returning the amount of letters that is = to your string input??? as it should return the amount of your inputed number.!!
This piece is wrong..
for(var i=0;i<myString.length;i++){
charactersToShow+=getChar+" "
as the one i have is returning the correct results..
for(var i=0;i<myNumber;i++){
charactersToShow+=getChar+" "
Whats wrong is the bit..<myString.length as to being <myNumber..
And follow how to add the start button and leave it out now.
Memm.:thumbsup:
dumpfi
02-05-2006, 06:53 PM
Its returning the amount of letters that is = to your string input??? as it should return the amount of your inputed number.!!... [The script] should output a version
of the input string which takes the form of a string of the same length
as the input string ...dumpfi
That is where I went wrong when I misunderstood the question.
Here's what is in your original question
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.
For example, if the user inputs the string abracadabra followed by the
number 7 then the output should be the string ddddddddddd
So you can use either of the following depending on which output you want to show
for(var i=0;i<myString.length;i++){ // same length as input string
charactersToShow+=getChar+" "
or
for(var i=0;i<myNumber;i++){ // same length as input number
charactersToShow+=getChar+" "
MemmnocK
02-05-2006, 09:14 PM
My Appologies Guys...:o :o :o
Yous are correct as i have misunderstood the question as i thought the output should be the length of the number input and not the correct length of the string input..:o
But it is good that i saw how to output the 2 versions..:D and understand how to do this now..so thanks guys and hope to see yous around here..:thumbsup:
Memm.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.