Do you know JavaScript?

Discussion in 'Code Snippets and Tutorials' started by NoPoint, Jun 25, 2007.

Thread Status:
Not open for further replies.
  1. NoPoint

    NoPoint Level III

    Joined:
    May 24, 2007
    Messages:
    621
    Likes Received:
    2
    Location:
    Somewhere on the Planet.
    Amend this 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.
    For example, if the user inputs the string abracadabra followed by the number 7 then the output should be 1. Similarly, if the user inputs the string abracadabra followed by the number 2 then the output should be 2.
    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.

    This is what I got so far:

    var userString;

    var userNumber;
    var index;

    userString = window.prompt('Enter a word of 8 or more letters', '');
    while (userString < 8)
    {
    userString = window.prompt('Word must be 8 or more letters', '');

    };

    document.write('The chose word was' + userString);
    userNumber = window.prompt('Enter a number between 1 and ' + userString.length, '');
    userNumber = parseFloat(userNumber);
    while (userString.length < 8)
    {
    userNumber = window.prompt('Number must be betwwen 1 and ' + userString.length, '');
    userNumber = parseFloat(userNumber)
    };
    document.write('Number chose was ' + userNumber, '');
    index = 0;
    while (userString.charAt(index) != userNumber)
    {
    index = index + 1
    };

    What do I do now? I get so far and then get totally stuck. Really starting to dislike javascript.
     
  2. tac123

    tac123 Level I

    Joined:
    Apr 29, 2007
    Messages:
    124
    Likes Received:
    0
    There were actually quite a few logic changes that needed to be made. Here is a summary, but you should definitely compare the code line for line if you want to really learn what is different:

    The first while statement needed to check that the length of the string was less than 8, rather than the string itself which is a series of characters and not a number. So this was changed to userString.length < 8.

    The second while statement needed to check for a number that was not in the proper range to be a character in the string. This means that if the number is less than 1 or greater than the length of the string, then the number is out of bounds. So the check for this was changed to userNumber < 1 || userNumber > userString.length, where the "||" means "Or".

    The last thing to note is that the function <string>.charAt(<number>) takes a number in the range [0... <string>.length - 1]. This means that for the 8 letter long string 'MoocowmO' <string>.charAt(0) corresponds to the first "M". The call <string>.charAt(1) returns 'o', charAt(7) is the very last 'O'. And most importantly charAt(8) does not even exist because it is past the end of the string.

    So it is arranged in the following way:
    0 -> 'M'
    1 -> 'o'
    2 -> 'o'
    3 -> 'c'
    4 -> 'o'
    5 -> 'w'
    6 -> 'm'
    7 -> 'O'

    Beyond that they do not exist.

    So by your prompt asking for a number starting at 1, it looked like you wanted the number 1 to correspond to the first letter 'M'. In order to do this we have to subtract 1 from every number the user enters. So if you enter a 1, then we subtract 1 from it and it is now 0, which is 'M'.

    So I changed the last chunk of code to:

    document.write('Character At (' + userNumber + ') is ' + userString.charAt(userNumber - 1));

    Here is all of the code:
    Code (Text):
    1.  
    2. var userString;
    3. var userNumber;
    4.  
    5. userString = window.prompt('Enter a word of 8 or more letters', '');
    6. while (userString.length < 8)
    7. {
    8.   userString = window.prompt('Word must be 8 or more letters', '');
    9. };
    10.  
    11. document.write('The chosen word was ' + userString + '<br>');
    12. userNumber = window.prompt('Enter a number between 1 and ' + userString.length, '');
    13. userNumber = parseFloat(userNumber);
    14. while (userNumber < 1 || userNumber > userString.length)
    15. {
    16.   userNumber = window.prompt('Number must be between 1 and ' + userString.length, '');
    17.   userNumber = parseFloat(userNumber)
    18. };
    19. document.write('Number chosen was ' + userNumber + '<br>', '');
    20.  
    21. document.write('Character At (' + userNumber + ') is ' + userString.charAt(userNumber - 1));
    22.  
     
Thread Status:
Not open for further replies.