Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Location
    NIreland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with a character count

    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.

    Code:
    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.

    Code:
    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 )

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    197
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your for loop try count++ instead of count=count+1
    Animation Rule #64
    Poor quality images are often artistic,
    Poor quality sound is ALWAYS annoying.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    197
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you have your if statement doing nothing

    if (condition) ;
    dosomething

    you need

    if (condition) {dosomething;}
    Animation Rule #64
    Poor quality images are often artistic,
    Poor quality sound is ALWAYS annoying.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by ez4ne12c View Post
    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

  • #5
    New to the CF scene
    Join Date
    Jun 2007
    Location
    NIreland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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 )


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •