Forum Moderators: open

Message Too Old, No Replies

I am a JavaScript newbie kindly help correct my code

         

java_junkie

12:14 am on Jun 15, 2007 (gmt 0)

10+ Year Member



<SCRIPT language="JavaScript" >
var userInput;
var userNumber;
var upperLimit = 20;
userInput = window.prompt('Please enter a number in the range 1 to ' + 20,'');
userNumber = parseFloat(userInput);
while ((userNumber <= 1) ¦¦ (userNumber >= upperLimit))
{
UserInput = window.prompt('Please re-enter - number should be in range 1 to + 20, '')
}
document.write('<BR>'+'Your chosen number was ' + yourNumber)
</SCRIPT>

My intension is to be able to store in a variable, a numerical value, to be entered by the user, in range from 1 to 20 determined by another variable. I have gotten no bearing kindly be of help...thanks in advance

phranque

2:42 am on Jun 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, j_j!

i'm no js junkie, but this may be closer to what you want:

<SCRIPT language="JavaScript" >
var userInput;
var userNumber;
var upperLimit = 20;
userInput = window.prompt('Please enter a number in the range 1 to + ' + upperLimit,'');
userNumber = parseFloat(userInput);
while ((userNumber < 1) ¦¦ (userNumber > upperLimit))
{
UserInput = window.prompt('Please re-enter - number should be in range 1 to + ' + upperLimit, '')
userNumber = parseFloat(userInput);
}
document.write('<BR>Your chosen number was ' + userNumber)
</SCRIPT>

Fotiman

3:31 pm on Jun 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




<SCRIPT language="JavaScript" >

1. Use all lowercase tag names. "script", not "SCRIPT".
2. Don't use the language attribute. It's not valid. Instead use the type attribute which is required.

<script type="text/javascript">


var userInput;
var userNumber;
var upperLimit = 20;
userInput = window.prompt('Please enter a number in the range 1 to ' + 20,'');
userNumber = parseFloat(userInput);

1. window.prompt is disabled by default in IE7.
Generic Spoofing Risk Reduction in Internet Explorer 7--The window.prompt script method is blocked and the gold Information bar is displayed by default in Internet Zone for Internet Explorer 7. The helps prevent websites from spoofing things such as the logon screens of other websites. This is a new security enhancement for Internet Explorer 7.
So, you might not want to use window.prompt any more.
2. You would be better off using your upperLimit variable for the value in the prompt text, and adding another variable for the lower limit.
3. If you want an alternative to window.prompt, I recommend the Yahoo UI Library [developer.yahoo.com]'s SimpleDialog [developer.yahoo.com] Component.

var lowerLimit = 1;
var upperLimit = 20;
userInput = window.prompt('Please enter a number in the range ' + lowerLimit + ' to ' + upperLimit, '');


while ((userNumber <= 1) ¦¦ (userNumber >= upperLimit))

1. If 1 and 20 are valid values (as your prompt text suggests), then your conditions are wrong. Should be:

while ((userNumber < 1) ¦¦ (userNumber > upperLimit))

or, if you add the lowerLimit variable as I suggested:

while ((userNumber < lowerLimit) ¦¦ (userNumber > upperLimit))


{
UserInput = window.prompt('Please re-enter - number should be in range 1 to + 20, '')
}

1. JavaScript is case sensitive. You've just created a new variable because you capitalized the "U" of "userInput". This could put you in an endless loop.
2. Again, use variables for the range.
3. You're missing closing quote in your message.
4. You're missing the semicolon at the end of the statement.

userInput = window.prompt('Please re-enter - number should be in range ' + lowerLimit + ' to ' + upperLimit, '');


document.write('<BR>'+'Your chosen number was ' + yourNumber)

1. document.write is considered 'sloppy'. I would avoid using it in favor of the DOM methods for writing to the document.
2. <BR> is considered presentational. You should avoid it.
3. You're doing extra concatination (between your <br> and the text) that you don't need to do.
4. This entire script is executed somewhere within your markup. Instead, it's better to keep a clean separation of your markup and your 'behavior' (or JavaScript). A better way to do this would be to wrap all of this in a function call and execute it on window.load, and then use the DOM methods to write the output to some place on your page.

However, for the sake of getting you up and running, just leave the document.write for now.

document.write('<br>Your chosen number was ' + yourNumber);


</SCRIPT>

1. Lowercase tags.

</script>

phranque

8:13 pm on Jun 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



ok here's another try, fixing a couple of things fotiman pointed out as well as a thing or two he missed, but not any of the "architecture" issues:

<script type="text/javascript">
var userInput;
var userNumber;
var lowerLimit = 1;
var upperLimit = 20;
userInput = window.prompt('Please enter a number in the range ' + lowerLimit + ' to ' + upperLimit,'');
userNumber = parseFloat(userInput);
while ((userNumber < lowerLimit) ¦¦ (userNumber > upperLimit))
{
userInput = window.prompt('Please re-enter - number should be in range ' + lowerLimit + ' to ' + upperLimit, '');
userNumber = parseFloat(userInput);
}
document.write('<BR>Your chosen number was ' + userNumber);
</script>

Fotiman

8:25 pm on Jun 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




as well as a thing or two he missed

Such as:
- parsing the float at the 2nd window.prompt
- the document.write method was referring to the wrong variables (yourNumber)

[edited by: Fotiman at 8:28 pm (utc) on June 15, 2007]

java_junkie

10:07 pm on Jun 15, 2007 (gmt 0)

10+ Year Member



Thanks, I have made amendment to the script and it working perfectly now...

java_junkie

1:57 pm on Jun 17, 2007 (gmt 0)

10+ Year Member



The corrected script isn't running

phranque

5:25 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



we won't be able to help you with a script that simply "isn't running".
try the javascript console in firefox and maybe you'll get some clues to the problem.
any print statements you add to the code will appear here.

java_junkie

10:07 pm on Jun 17, 2007 (gmt 0)

10+ Year Member




System: The following 5 messages were spliced on to this thread from: http://www.webmasterworld.com/javascript/3370427.htm [webmasterworld.com] by engine - 9:44 am on June 18, 2007 (utc +1)


Write a program so that it will prompt for, and accept from the user, an input string which contains at least eight 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 the number of occurrences in the input string of the letter which occurs at the position specified by the input number.

Logician

1:55 am on Jun 18, 2007 (gmt 0)

10+ Year Member



Why would I or anyone else want to do that?

java_junkie

6:29 am on Jun 18, 2007 (gmt 0)

10+ Year Member



I am new to JavaScript and I have tried it myself but I couldn't go any further that was why I asked for help

java_junkie

6:36 am on Jun 18, 2007 (gmt 0)

10+ Year Member



Below is what I have been able to do with the question:

<SCRIPT LANGUAGE = "JavaScript">

var inputString; // string entered by user

var inputNumber; // Number entered by user

// initialise inputString to an empty string
inputString = '';

inputString = window.prompt('Please input a string which contain at least eight characters', '');
inputString = parseFloat(inputString);

while (myString < inputString.length)
{
inputString = window.prompt('lease re-enter a string which contain at least eight characters', '');

document.write('The length of ' + inputString + ' is ' + inputString.length + '<BR>')
};
document.write('The number of occurrences was' + '<BR>')

</SCRIPT>

daveVk

7:44 am on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Insertions show in bold, deletions in red. Not sure what you are trying to count the occurrences of, code for that not included.

<SCRIPT LANGUAGE = "JavaScript">
<script type="text/javascript">
var inputString; // string entered by user

var inputNumber; // Number entered by user

// initialise inputString to an empty string
inputString = '';

while ( inputString.length < 8 ) {
inputString = window.prompt('Please input a string which contain at least eight characters', '');
}
inputString = parseFloat(inputString);
while (myString < inputString.length)
{
inputString = window.prompt('lease re-enter a string which contain at least eight characters', '');

document.write('The length of ' + inputString + ' is ' + inputString.length + '<BR>')
};
document.write('The number of occurrences was' + '<BR>')

</SCRIPT>

java_junkie

3:48 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



Thanks DaveVK the script is responding now and I have noted my mistakes but according to the question I was asked to do two things: Write a program so that it will prompt for, and accept from the user, an input string which contains at least eight 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 the number of occurrences in the input string of the letter which occurs at the position specified by the input number.

daveVk

2:21 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One step at a time.

It should then prompt for, and accept from the user, a numerical value that is no greater than the length of the input string

var num = 0;
var numString = "";
while ( num <= inputString.length ) {
numString = window.prompt('Enter number blah blah', '')
if ( isNaN( parseFloat(numString) ) === false ) { num = parseFloat(numString); }
}

java_junkie

5:30 am on Jun 19, 2007 (gmt 0)

10+ Year Member



Thanks DaveVK, the script is now request that the user enter a numerical value that is not greater than the input string. It remains only the finally step...

daveVk

6:18 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. Call substring to get character in question.
2. Repeatably call indexOf to count occurences.

java_junkie

6:19 am on Jun 19, 2007 (gmt 0)

10+ Year Member



DaveVK sorry if I am asking all this question I am totally new to all this thing, I hope it isn't embarrassing to ask because I don't know how to do that

java_junkie

6:32 am on Jun 19, 2007 (gmt 0)

10+ Year Member



It's I better I ask than pretend that I know your guidelines will help me improve and be a better coder thanks for the help

daveVk

11:39 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var ch = inputString.substr( num-1, 1 );
var count = 0;
var inx = -1;
while ( (inx=inputString.indexOf(ch,inx+1)) > 0 ) { count++; }

java_junkie

12:01 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



The script looks like this now:

<script type="text/javascript">
var inputString; // string entered by user

// initialise inputString to an empty string
inputString = '';

while ( inputString.length < 8 ) {
inputString = window.prompt('Please enter a string which contain at least eight characters', '');
}

var num = 0;
var numString = "";
while ( num > inputString.length ) {
numString = window.prompt('Enter a numerical value that is not greater than the lenght of the input string', '')
if ( isNaN( parseFloat(numString) ) === false ) { num = parseFloat(numString); }
}

var ch = inputString.substr( num-1, 1 );
var count = 0;
var inx = -1;
while ( (inx=inputString.indexOf(ch,inx+1)) > 0 ) { count++; }

document.write('The number of occurrences of' + inputString.substr + ' is ' + inputString.count + '<BR>')

</script>

java_junkie

12:04 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



And this is the output that it returns when I run the script I posted above : The number of occurrences offunction substr() { [native code] } is undefined

Bernard Marx

12:39 pm on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var promptForString = "Please enter a string which contains at least eight characters";
var promptForNumber = "Enter a numerical value that is not greater than ";
var inputString = '';
var inputNumber = 0;
var character = "";
var characterCount = 0;
var characterIndex = 0;

while ( inputString.length < 8 ) {
inputString = prompt( promptForString,'');
}

while ( inputNumber==0 [red][b]¦¦[/b][/red] inputNumber > inputString.length ) {
inputNumber = prompt( promptForNumber+inputString.length, '')*1;
if ( isNaN( inputNumber )) continue;
}

character = inputString.charAt(inputNumber-1);


while ( (characterIndex = inputString.indexOf(character,characterIndex+1)) > 0 )
{ characterCount++; }

document.write(
"The number of occurrences of " + character
+" in "+inputString+" is " + characterCount + '<BR>'
);

java_junkie

1:15 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Thanks Bernard the script is functioning as expect kindly try to run the script with bcsbdbcbcsb and when it prompt for a numerical value try to enter 4 the output will then be 4 which isn't right because the output is meant to be 5 because b occurs 5times in the input string bcsbdbcbcsb is there something that isn't included in the script?

Bernard Marx

1:21 pm on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, I must have messed up. I'll have a look.

java_junkie

1:24 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Thank you for having a look you and DaveVK has been helpful

Bernard Marx

1:54 pm on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



2 changes:

1) Initialise characterIndex = -1 (as it was originally)

2) characterIndex = inputString.indexOf(character,characterIndex+1)) > -1
(or the loop will exit if the character is in 0th position)

[edited by: Bernard_Marx at 1:56 pm (utc) on June 19, 2007]

java_junkie

1:59 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Yes! a big thank you Bernard the script fully functional now and now I proceed to the interesting part of JavaScript. Thanks for your support