...

View Full Version : Help with a character count



flowswithdrek
06-11-2007, 11:54 PM
Firstly Hi All

Im a javascript newbie and Im having a problem with an assignment, I am not looking for someone to do the work for me. I already have 99% of it done and working as I want it to but the last bit is eluding me and is frankly driving me nuts. I hate to be beaten but ive wasted a lot of time on this one and cant afford to waste much more. I just need someone to point me in the right direction or tell me what im doing wrong, or why my code is doing the wrong thing, any help would be very much appreciated.

The code is supposed to do the following.

Prompt the user to enter a string of eight or more characters, if less than eight characters are entered the user is prompted again using a while loop (this is working)

Prompt the user to enter a number not greater than the string length, if the number is greater the user is prompted again using a while loop (this is working)

Identify the character in the string that corresponds to the number input. Eg javascript has 10 characters and the second character is a. (this is working, I tested this by getting the program to write the character on the screen)

Calculate the number of times the identified character appears in the string and display this number. Eg a appears twice in the string javascript. This is the problem area, my for code appears to be counting correctly but displaying the number of characters in the string +1 instead of the number of occurrences of the character corresponding to the number input. Iíve had the program display the count so I know its working as expected.

So the problem bit of the code seems to be the following.



for (var count = 0; count <=userInput.length; count = count +1)
{

if (character = = userInput.charAt(count));

output = output + 1
}

document.write( '<br>'+ output )


my code is not throwing any errors so it must be doing what ive told it to do, im obviously telling it to do the wrong thing.

The full code is.



var userInput;
var userNumber;
var upperLimit = 8;
var character;
var output = 0;
output = parseFloat(output)

userInput = window.prompt('Please enter a string with at least ' + upperLimit + ' characters','');




while ((userInput.length <1) || (userInput.length < upperLimit))
{
userInput = window.prompt('Please re-enter - string should have at least ' + upperLimit + ' characters','')

}

userNumber = window.prompt('Please enter a number no greater than the length of string','')
userNumber = parseFloat(userNumber)


while (userNumber>userInput.length)
{
userNumber = window.prompt ('Please re-enter your number','')
userNumber = parseFloat(userNumber)
}




character = userInput.charAt(userNumber-1);

for (var count = 0; count <=userInput.length; count = count +1)
{

if (character = = userInput.charAt(count);

output = output + 1
}

document.write( '<br>'+ output )

ez4ne12c
06-12-2007, 02:48 AM
In your for loop try count++ instead of count=count+1

ez4ne12c
06-12-2007, 02:50 AM
you have your if statement doing nothing

if (condition) ;
dosomething

you need

if (condition) {dosomething;}

Philip M
06-12-2007, 07:36 AM
In your for loop try count++ instead of count=count+1

That is perfectly OK, although count++ is more usual.

for (var count = 0; count <= userInput.length; count = count +1) { // or count++
if (character = = userInput.charAt(count) {
output = output + 1 // or output++
} // end of if statement
document.write( '<br>'+ output );
} // end of for loop

flowswithdrek
06-12-2007, 09:05 PM
Cheers for the input guys, i have it sorted and outputing the correct number of characters in the string. and count ++ is also a useful hint.

final code looks like this


var userInput;
var userNumber;
var upperLimit = 8;
var output = 0;
output = parseFloat(output)

userInput = window.prompt('Please enter a string with at least ' + upperLimit + ' characters','');




while ((userInput.length <1) || (userInput.length < upperLimit))
{
userInput = window.prompt('Please re-enter - string should have at least ' + upperLimit + ' characters','')

}

userNumber = window.prompt('Please enter a number no greater than the length of string','')
userNumber = parseFloat(userNumber)


while (userNumber>userInput.length)
{
userNumber = window.prompt ('Please re-enter your number','')
userNumber = parseFloat(userNumber)
}




for (var count = 0; count <=userInput.length; count = count +1)

{

{
if (userInput.charAt(count) == userInput.charAt(userNumber-1))
output = output+1
}
}

document.write( '<br>'+ output )



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum