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
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT