jeudi 1 mars 2018

How to implement Else If, For, While Loops, Length, Sort Arrays all at the same code

Hello everyone,

I've experienced an issue with solving one of the assignments that I've been assigned to!

The Assignment:

Write your Own Code Create a program in JavaScript to implement the following 15 requirements. Please save the given 15requirements in a single file.

Requirement_1. Prompt the user to enter one number within the range from 0 to 1000. You will also need to capture the user input in a variable named r1

Requirement_2. Verify if the user input is a number.

Requirement_3. Print the value of r1 (use document.write). • Hint: Here I want to provide more explanation about the word print; print also means display to the user.

Requirement_4. Declare a new variable and name it r4. Next, initialize r4 to the value of variable r1 incremented by 5. Finally, print the value of r4.

Requirement_5. Declare a new variable and name it r5. Next, initialize the variable to the value of variable r1 multiplied by 7. Finally, print the value of r5.

Requirement_6. Create an array of 3 floats and name this array r6. Initialize the array r6 with the values of three var, ables r1, r4, and r5.

Requirement_7. Print the elements of the array r6.

Requirement_8. Using a for loop to add the following elements at the end of the array r6: 1 2 3 4 5 6 7 8 9 10 .... up to 101 Print the all elements of the array.

Requirement_9: Use the length property to find out the size of the array. Print the size of the array.

Requirement_10: Sort the array (numerical order). Print the sorted array. Requirement_11: Using a for loop print the array elements one after another with a linebreak between them.

Requirement_12: Find the value of the middle element in the sorted array (first middle element). Print this value.

Requirement_13: Using a for loop print the elements of the sorted array up to the middle element value. Once the loop reaches the cycle of the middle element, print the elements in descending order, from current value back to index 0.

Requirement_14: Find and print the min value r15Min and max value r15Max of the r6 array.

Requirement_15: Generate a table using a for loop. The table has r15Min rows and r15Max columns. Write in each table cell the corresponding row number and the column number.

Here is what I have tried so far:

     /*
    Create a program in JavaScript to implement the following 15 requirements. Please save the given 15requirements in a
    single file.

 */


  //  Requirement_1.  Prompt the user to enter one number within the range from 0 to 1000
  var r1 = prompt("Please enter one number within the range from 0 to 1000");

                // Requirement_2.  Verify if the user input is a number
              if (isNaN(r1)) {
                 alert("You have entered Not a Number \nPlease enter the number in given range!");
                  } // Verify if the number within the range from 0 to 1000
                     else if (!(r1 >= 0 && r1 <= 1000)) {
                             alert("This number: " + r1 + " is out of given range"); 
                      } // Requirement_3.   Print the result that is entered by User
                        else {
                       document.write("The user has entered a number: " + r1 + "<br/>");
                      }

  // Requirement_4.   Declaring a new variable and name it r4
  var r4;  

  // Next, initializing r4 to the value of variable r1
  r4 = r1;

               // Can't figure this out Should I use a For Loop or Other Statement
            while ( r4 == r1) {
                 document.write("The value of r4 incresed by five:  " + r4 + "<br/>");  // print the value of r4
                 r4+5   // incremented by 5
              }

 // Requirement_5.  Declaring a new variable and name it r5.
 var r5;

 // Next, initializing r4 to the value of variable r1
 r5 = r1;

            // Can't figure this out Should I use a For Loop or Other Statement
            while (r5 == r1) {
                document.write("The value of r5  multiplied by seven:  " + r5 + "<br/>");
                     // multiplied by 7
            }


  // Requirement_6. Create an array of 3 floats and name this array r6  
  // is that correct?     
  var r6 = [ 1.0, 2.0, 3.0 ];          


           //  Initialize the array r6 with the values of three variables r1, r4 and r5.
           // is that correct?

           r6[1.0] = r1;
           r6[2.0] = r4;
           r6[3.0] = r5;


           // Requirement_7. Print the elements of the array r6.
           // is that correct?
            document.writeln( r6[ ] );


         // Requirement_8. Using a for loop to add the following elements at the end of the array r6:
         // 1 2 3 4 5 6 7 8 9 10 .... up to 101
         // Print the all elements of the array.
         // Can't figure this out;
         for () {

         }


 // Requirement_9: Use the length property to find out the size of the array. Print the size of the array.

        document.writeln( "Length of array is: " + r6.length + "<br/>");

 //Requirement_10: Sort the array (numerical order). Print the sorted array.
 // is that correct?

        r6.sort(function(a, b) {
         return a - b
         });;
         document.writeln(r6 + "<br/>");

  // Requirement_11: Using a for loop print the array elements one after another with a linebreak between them.
  // Can't figure this out;



 // Requirement_12: Find the value of the middle element in the sorted array (first middle element). Print this value.
 // Can't figure this out;


 //Requirement_13: Using a for loop print the elements of the sorted array up to the middle element value. Once the loop
 //reaches the cycle of the middle element, print the elements in descending order, from current value back to index 0.
 // Can't figure this out;


 // Requirement_14: Find and print the min value r15Min and max value r15Max of the r6 array.
 // Can't figure this out;


 // Requirement_15: Generate a table using a for a loop. The table has r15Min rows and r15Max columns. Write in each
 // table cell the corresponding row number and the column number.
 // Can't figure this out;

As you can see that I have commented out the one that I have a problem with or the one that I've tried to solve but don't know if I'm doing it right!

Please, anybody with experience in JavaScript help me with that type of assignment or at least give me a right direction on how to make it work as it requires from me!

Thank you

Aucun commentaire:

Enregistrer un commentaire