Question

In: Computer Science

How do you write JavaScript codes to randomly select five elements by using a single for-loop?...

How do you write JavaScript codes to randomly select five elements by using a single for-loop? How do you display the results to a html page?

var password = [“x”, “y”, “z”, “1”, “2”, “3”, “test”];

Sample output:

zx21y

Solutions

Expert Solution

HTML file :

<!DOCTYPE html>

<html lang="en">

<head>

    <!--  -->

    <title>Javascript elements</title>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

    <!-- script is used for javascript -->

    <script>

        //Javascript array

        var password = ["x", "y", "z", "1", "2", "3", "test"];

        //using for loop

        for (var i = 0; i < 5; i++) {

            //randomly generate a number from 0 to array length

            var randomNumber = Math.floor((Math.random() * password.length - 1) + 1);

            //print the numbers on the web page

            document.write(password[randomNumber]);

        }

    </script>

</body>

</html>

=========================================

Screen showing random numbers :


Related Solutions

Based on the descriptions given below, write the JavaScript codes to calculate the amount to be...
Based on the descriptions given below, write the JavaScript codes to calculate the amount to be paid for books purchased.  Declare all the variables used in this program.  Ask the user to key-in the book code using a prompt() method. Store the value in a variable named book_code.  Ask the user to key-in the number of books purchased using a prompt() method. Store the value in a variable named book_qty.  Ask the user whether they have...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
I want to compare a string with the previous strings in a forEach loop using javascript....
I want to compare a string with the previous strings in a forEach loop using javascript. So im accessing data from an API. Im iterating through them using a forEach() loop. I want to compare the current 'title' string from the api with all previous title strings its pulled. The title is located under article.title fetch(req)             .then((response) => {                 return response.json();             })             .then((data) => {                 console.log(data);                 data.articles.forEach((article) => { } If the title matches any other title used already id...
write a program bus management system? using: if else, for loop, do while loop, function, arrays,...
write a program bus management system? using: if else, for loop, do while loop, function, arrays, string, structure
what is dynamic document in javascript. why this is important. how can you position elements in...
what is dynamic document in javascript. why this is important. how can you position elements in a dynamic document, how can you create dynamic content, what actions take place when the user clicks a mouse.
How do we return the datatype of arguments without using type of in javascript
How do we return the datatype of arguments without using type of in javascript
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
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...
Ask the user how many days that they collected gems. A loop you write should loop...
Ask the user how many days that they collected gems. A loop you write should loop how many days the user enters. If the user enters 5 days, then the loop should loop 5 times to collect the data from each day. So, each loop iteration represents a day. In each loop iteration, ask the user how many gems they collected that day. After the loop finishes gathering the data for each day, calculate the total and average gems collected....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT