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

How do you write this code in JavaScript inside the head? Prompt the user for their...
How do you write this code in JavaScript inside the head? Prompt the user for their first name. Remember you must save the value that the user enters so that you can use that value, therefore the prompt must be in an assignment statement. Prompt the user for their last name. Have an alert box pop up that contains the first name followed by a space and then the last name followed by a short message that you make up....
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, test, and debug (if necessary) HTML file with the Javascript codes in an external file...
Write, test, and debug (if necessary) HTML file with the Javascript codes in an external file for the following problem: Input: a number, n, using prompt, which is the number of the factorial numbers to be displayed. Output: a bordered table of numbers with proper caption and headings in which the first column displays the numbers from 1 to n and the second column displays the first n factorial numbers. For example, if a user enters 10 to be the...
Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a...
Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline.
You are to write a program in C to do the following in a loop for...
You are to write a program in C to do the following in a loop for the KL46Z . Prompt the user for a positive integer greater than 1 and sanity-check the input. If the number is a prime number, it is to be printed on a new line in red text. If the number is evenly divisible by 7, it is to be printed on a new line in green text. If the current number is evenly divisible by...
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
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
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT