Javascript program to prompt and accept input from user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lisaj
    New Member
    • Mar 2006
    • 8

    Javascript program to prompt and accept input from user

    I'm having huge difficulties producing a script for this:
    Write a javascript programme 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 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.

    Been trying for days and just can't get it!!! :(((
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If you post the code of what you have so far we will try to help you, but since this sounds to me like a homework question I'm not just doing it for you.

    BTW this would be better off in the Programming or Web Design forum.

    Comment

    • lisaj
      New Member
      • Mar 2006
      • 8

      #3
      Originally posted by Banfa
      If you post the code of what you have so far we will try to help you, but since this sounds to me like a homework question I'm not just doing it for you.

      BTW this would be better off in the Programming or Web Design forum.
      <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.lengt h < 8)
      {
      alert("Please re-enter a Word with more than 8 Characters !") // tempory alert
      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="getStri ngInput()">

      <div id="display"></div>

      </BODY>
      </HTML>
      This is what i've done so far.....

      Comment

      • lisaj
        New Member
        • Mar 2006
        • 8

        #4
        Javascript program to prompt and accept input from user

        I'm having huge difficulties producing a script for this:
        Write a javascript programme 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 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.

        Been trying for days and just can't get it!!! ((

        this is what i have so far.....
        [HTML]<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.lengt h < 8)
        {
        alert("Please re-enter a Word with more than 8 Characters !") // tempory alert
        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="getStri ngInput()">

        <div id="display"></div>

        </BODY>
        </HTML>[/HTML]

        Comment

        • lisaj
          New Member
          • Mar 2006
          • 8

          #5
          And again...

          <HTML>
          <HEAD>
          <TITLE>Program_ TMA03
          </TITLE>
          <SCRIPT >

          /* Program to read in a known number of data items and store them in an array */

          var attendArray = new Array (5);
          var dayArray = ['Monday','Tuesd ay','Wednesday' ,'Thursday','Fr iday']


          document.write( 'Array program to read in a known number of data items');
          for (var day = 0; day < attendArray.len gth; day = day + 1)
          {
          attendArray[day] = window.prompt(' Enter attendance value for ' + (dayArray + 1),'')
          };
          document.write( '<BR>' + '<BR>');
          document.write( 'Confirmation of data input' + '<BR>' + '<BR>');

          for (var day = 0; day < attendArray.len gth; day = day + 1)
          {
          document.write( dayArray[day] + ' : ' + attendArray[day] + '<BR>')
          }

          </SCRIPT>
          </HEAD>
          <BODY>
          </BODY>
          </HTML>

          When I execute this programme I need the value button to have the day on;
          eg; Enter the attendace for Monday
          Next: Enter the attendance for Tuesday

          I can only seem to get it to write up all of the days of the week at once. Can anyone tell me why?

          Hopefully waiting....

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            OK the problem you have is the order that you have declared your functions. In Javascript you need to have declared a function before you try to call it. You declare the functions in the order

            getStringInput
            getNumber
            showResults

            but getStringInput calls getNumber and getNumber calls showResults. You need to delare them in the order

            showResults
            getNumber
            getStringInput

            This is the sort of feature you often get in an interpreted (as opposed to compiled) language.

            On the contents of the function getStringInput, the recursive calling works but in this case since it could be replaced with a simple do ... while loop. In pseudo code this would look something like

            Code:
              do
              {
                string = GetInputFromUser;
            
                if ( string.length <= 8 )
                {
                  Display Error Message;
                }
              } while( string.length <= 8 );
            Just in case you are wonder psudo code is code that shows the structure of what needs to be done without neccessaryily being runnable code of a given language.

            1 final note, the question calls for a string of at least 8 characters and the logic you have code tests for a string of at least 8 characters but your error messages says

            "... more than 8 Characters ..."

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              When you say the value button I assume you mean window prompt box.

              attendArray[day] = window.prompt(' Enter attendance value for ' + (dayArray + 1),'')

              should be

              attendArray[day] = window.prompt(' Enter attendance value for ' + dayArray[day]);

              Make sure you understand why.

              Comment

              • lisaj
                New Member
                • Mar 2006
                • 8

                #8
                Thankyou

                Originally posted by Banfa
                When you say the value button I assume you mean window prompt box.

                attendArray[day] = window.prompt(' Enter attendance value for ' + (dayArray + 1),'')

                should be

                attendArray[day] = window.prompt(' Enter attendance value for ' + dayArray[day]);

                Make sure you understand why.
                You are an absolute star ***** many thanks x

                Comment

                • m3rajk
                  New Member
                  • Mar 2006
                  • 8

                  #9
                  i really dislike doing other people's homework. but since you have tried, i will give you this clue:
                  you need 4 variables:
                  var input_string
                  var output_string
                  var is_length
                  var is_place

                  you should use onChange() to update your length

                  in psuedo-code:

                  Code:
                  <html start stuff>
                  <script language="javascript">
                  
                  // declare variables
                  
                  function valid(){
                    if (length<8){
                      alert("you must have 8 or more characters");
                      return FALSE;
                    }else{
                      alert("thank you for entering 8 or more characters");
                      return TRUE;
                    }
                  }
                  
                  function getPlace(){
                    while(place is not between 0 and length){
                      place=window.prompt("enter a number 0 to length");
                    }
                  }
                  that shouyld be a good starter

                  Comment

                  Working...