Question

In: Computer Science

For both JavaScript and HTML: ---------------------------------------------------- Write and test JavaScript scripts for the two exercises that...

For both JavaScript and HTML:

----------------------------------------------------

Write and test JavaScript scripts for the two exercises that follow, debug (if necessary).  When required to write a function, you must include a script to test the function with at least two different data sets.  In all cases, for testing, you must write an HTML file that references the JavaScript file.

  1. Output an HTML table of the numbers from 2 to 8 along with their squares and cubes.
  2. Create a function titled counter that takes an array of numbers as the parameter. The function is then to return an array holding the numbers of negative elements, zeros, and values greater than zero in the given array parameter.

Solutions

Expert Solution

// Code for question1

<!DOCTYPE html>
<html>

<body>


<!-- creating table with solid border -->
   <table style="border: solid;">

       <!-- creating row with three headers -->
       <tr>
          <th>Number</th>
          <th>Square</th>
          <th>Cube</th>
       </tr>

       <!-- creating rows with their three datas: number, square and cubes -->

       <tr>
          <td>2</td>
          <td>4</td>
          <td>8</td>
       </tr>

       <tr>
          <td>3</td>
          <td>9</td>
          <td>27</td>
       </tr>


       <tr>
          <td>4</td>
          <td>16</td>
          <td>64</td>
       </tr>

       <tr>
          <td>5</td>
          <td>25</td>
          <td>125</td>
       </tr>

       <tr>
          <td>6</td>
          <td>36</td>
          <td>216</td>
       </tr>

       <tr>
          <td>7</td>
          <td>49</td>
          <td>343</td>
       </tr>

       <tr>
          <td>8</td>
          <td>64</td>
          <td>512</td>
       </tr>


   </table>

</body>
</html>

// Screenshot for question1

// output for question1:

// QUESTION2 SOLUTION:

// counter.js file for question2 which has the implementation of counter function

// Creating a function counter which takes an array arr of number as a parameter

function counter(arr){

   // intializing counter variable of negative number, positive number and zero to 0
   var negativeCount = 0;
   var positiveCount = 0;
   var zeroCount = 0;

   var i;
   // start counting
   for(i=0; i < arr.length; i++){

       // if number is zero increment zeroCount
       if(arr[i] == 0)
           zeroCount++;

       // if number is negative increment negative count
       else if(arr[i] < 0)
           negativeCount++;

       // else increment positive count
       else
           positiveCount++;


      
   }

   // now create an array having all three count as elements
   //var tempArr = new Array(positiveCount, zeroCount, negativeCount);
   var tempArr = [];
   tempArr[0] = negativeCount;
   tempArr[1] = zeroCount;
   tempArr[2] = positiveCount;

   // now return the tempArr
   return tempArr;
}

// Screenshot for counter.js file

// Html code to test the counter function

<!DOCTYPE html>
<html>
<head>
   <!-- Import the counter.js file -->
   <script src="counter.js"></script>
</head>
<body>

   <h1>Test1 output: </h1>
   <p id="test1"></p>

   <h1>Test2 output: </h1>
   <p id="test2"></p>

   <!-- Writing script to check the counter function of couter.js file -->
   <script >
      

       // test 1
       var tempArr = counter([1, 2, 0, 0, 5, -1, -3, -4, 0, 4, 0, 0]);

       document.getElementById("test1").innerHTML = "Negative count is: " + tempArr[0] + "<br>" + "Zero count is: " + tempArr[1] + "<br>" + "Positive count is: " + tempArr[2];

       // test 2
       var tempArr = counter([13, 23, 9, 0, 5, -1, -3, -4, 0, 4, 0, 0, -99, 0, 76]);

       document.getElementById("test2").innerHTML = "Negative count is: " + tempArr[0] + "<br>" + "Zero count is: " + tempArr[1] + "<br>" + "Positive count is: " + tempArr[2];

   </script>

  

</body>
</html>

// screenshot for HTML test file of counter function

// OUTPUT for question2 after running html test file


Related Solutions

Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: Border size? 2px, 5px, or 8px. Border Color? blue, red, green, and black. Border style? solid, dotted, dashed, and double. The HTML document...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the...
Write, test, and debug (if necessary) JavaScript scripts for the following problem. You must write the HTML file that references the JavaScript file. Use prompt to collect names of persons from the user. When the user enters ‘DONE’, your script should stop collecting more names. Then, it should ask the user to specify the following style properties: - Border size? 2px, 5px, or 8px. - Border Color? blue, red, green, and black. - Border style? solid, dotted, dashed, and double....
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript...
This needs to be written in JavaScript with two files. One HTML file and a JavaScript file. Make an empty HTML file, put three buttons inside the body and an empty main tag below the buttons. The text of the buttons should be "Foo", "Bar", and "FooBar" respectively. Don't put the " in the buttons that's just for clarification in these instructions. Add unique IDs to each button In your JavaScript, get 3 separate references to the buttons using the...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML <canvas> tag. The game will be played "hot seat" where players take turns using the same device. Requirements: The canvas should be 600px tall and wide, with the gameplay area occupying most of the canvas. The X's and O's may be drawn using polygons or large-font text The grid should be drawn using polygons, specifically long, thin rectangles Before & between games, the canvas...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
Write an HTML/Javascript canvas program that displays a black dot in the center after the user...
Write an HTML/Javascript canvas program that displays a black dot in the center after the user presses the 's' button on the keyboard. Hint: Canvas size should be 500x500 and the black dot should only appear after pressing 's'.
The code to create a Search/Filter Data with Javascript or html from html page.
The code to create a Search/Filter Data with Javascript or html from html page.
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
Write a log-in log-out session using PHP, CSS, and HTML only. NO MYSQL and JavaScript allowed....
Write a log-in log-out session using PHP, CSS, and HTML only. NO MYSQL and JavaScript allowed. 1. Need an HTML form and a login form; separate file. 2. need a CSS file to make it look good 3. Must store visitors' info in a .txt file and validate from there if the user exists before granting access to the dashboard. If the user does not exist, render the form to signup. If the user exists take them to their dashboard....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT