
Concept explainers
Part 5: JavaScript - Displaying the Input
The next step is to display the user input on the HTML page. The input data is stored in the array, so each element in the array will become a separate line of output on the web page. Eventually, we will ensure the output is all upper case, and that each line of text is a different, random color. Below is an example of the final output, assuming the user input a, b, c, and then finally nothing.
To display the output, you will need to use another loop structure: for, while, or do...while. You choose. Before outputting to the browser, first, output to the console to ensure your looping works.
>> Use an appropriate loop structure to loop through the input data array, and output each array item to the console.
Now that you're working with the data, you might as well make adjustments to ensure each item is now displayed as upper case.
>> Make coding changes to display the input data output to the console as all upper case content.
Now that you know your loop and upper case coding works, you can now work on outputting the content to the HTML page. Although multiple solutions exist to solve this problem, you are required to use String concatenation to create a single string of text that contains all of the final output. You will use techniques you've used before to get a reference to the HTML output div, and update the content of the output div with this string of text.
>> Update your code to concatenate all of the array input data, all converted to upper case, and output the combined text using querySelector().
Tip: You can use either textContent, or innerHTML. In the next section, you'll note that to get the additional HTML formatting to display in the output, you will need to use innerHTML.
Did your text all come out on a single line? Probably. What can you do to solve that problem?
Part 6: JavaScript - Updating the Output
The output definitely does not look similar to the example output above. In order to solve this problem, you need to consider what the final HTML will look like, and then set that final HTML as your target.
To separate data onto separate lines in HTML, you need to either use a block element (e.g. div, p, h1), or the line break element, br. You will use the line break.
Tip: For this class, we will use the self-closing version of the break tag: <br />. The break tag is a void element (i.e. will never have content to enclose), and self-closing is often required by more advanced JavaScript web app solutions.
>> Use string concatenation to append a line break (<br />) after each input item. You will need to use a different property of the referenced div element to set the output. In the past, you've used textContent, but this property will not support additional format tags; instead, use innerHTML. See below.
const ref = document.querySelector('#output'); // Get output div reference
ref.innerHTML = stringOutput; // Use innerHTML to set the div element content, not textContent
Your output should now appear on separate lines.
The final update to the output is to display the output in different, randomly selected colors. To accomplish this change, we will declare an array of ten colors, and generate a random integer to select the color from the color array. This color will then be used in conjunction with the span tag, and an inline style using the color CSS property, to set each input color.
>> Add an array of HTML color names to the startup function. The array variable must be a constant array, and named colors. The array must have ten color names selected from the W3Schools HTML Color Names (Links to an external site.) page.
To accomplish the task of producing the correct HTML output, we need to visualize what the final HTML would like like. For example, given user-input of AAA, and a randomly selected color of Blue, below is the HTML we need to product.
<span style="color:blue;">AAA</span>
>> Update your code to use a span tag, and an inline style to set the random color using getRandomInt, for each input item from the data array. You will continue to concatenate all of the text together to make a single string of text. After this step, your output should resemble the sample output above.
Important Tip: You will undoubtedly not get the HTML correct the first time you attempt to loop, get a random color, and concatenate everything into a single string. See the next section about how to see the HTML div element contents.

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- JAVASCRIPT: Part I– For you “Login.html” page, we are going to do some Javascript validating. First validate that the user typed in something in both textboxes. If either textbox is empty, pop-up a message that tells the user that they must enter a Valid Customer ID and Password. Part II– Again for you “Login.html” page, validate that the Customer ID is a Number, not a string. Also all Customer IDs are numbers between 3000 and 3999. All 4 digit numbers. Validate that all Customer IDs are numbers between 3000 and 3999.arrow_forward7-Fully explain the following code and draw all possible outputs. JavaScript HTML DOM Finding HTML Elements by Tag Name This example demonstrates the getElementsByTagName method. const x = document.getElementById("main"); const y = x.getElementsByTagName("p"); document.getElementById("demo").innerHTML = 'The first paragraph (index 0) inside "main" is: y[0].innerHTML;arrow_forwardIn python, write a function own_filter where it can flip an image vertically or set an image to a red color.arrow_forward
- Topic: Server-Side Development (Control of User Input) In a paragraph, describe how you can control user input so it displays properly. For example, if someone uses <h1> in a text field, how are you going to prevent this from displaying as a level 1 heading when you later display this text?arrow_forwardUsing HTML and Javascript inside <body> section use tag to create a vector graphic area with 200 points as width and height and xmlns ="http://www.w3.org/2000/svg" and then create a blue circle of radius 50 at the location (100,100) inside. 5) Write JavaScript so that when the user clicks on the circle, the circle disappears.arrow_forwardJava Use GUI. Simple text editor Copy / Cut Paste Display number of characters (with spaces / without spaces) Display number of words Display number of linesarrow_forward
- A student report this problem when using HTML and JavaScript "On the assignment with preloaded images, I had to rewrite the example from the book. I had a problem with the syntax for linking the source file to the <form> element. I solved the problem by using this syntax document images img3.src = (source of file)." What kind of error is it? What rules were applied, and which should have been applied earlier?arrow_forwardWrite a for-loop that will write to the HTML document the value of each element in the array called myArray. Note: You only need to write a for-loop. Code on the outside of the for-loop is not necessary as we will assume that myArray has been declared and initialized with a series of array elements.arrow_forwardx = zeros(uint8(5,5)); for i = 1:5 for j = 1:5 a = 5/i; x(i,j) = 255/a; end end Which of the following best describes the black and white image x? 1. The columns of x will get lighter from left to right 2. The rows of x will get lighter from top to bottom 3. The rows of x will get darker from top to bottom 4. The columns of x will get darker from left to rightarrow_forward
- Write a JavaScript code with the following three different conditions: • Create the HTML document containing an element with id="myImage" for any images you want. • Use the HTML DOM to get the element with id="myImage". • Write a JavaScript function to change the JavaScript src attribute of element from " pet.gif" to " cat.jpg" when clicking on a button. Note: Students can use any gif and jpg files according to their choice. Important notes: 1. You should have to copy and paste the script as your answer to this question. DON’T take a screenshot for your script. It must be an editable script. 2. Take a screenshot of your output web page and paste it as a part of your answer.arrow_forwardHTML and CSSarrow_forwardProblem 2: Using HTML/JavaScript, find and display the number of multiples of a certain number entered by the user. First have the user enter a number in a prompt box from 1-10. Check if they enter a valid number - if they don't, prompt them again. Use a loop for this. Next, use a second loop to have the user enter a number in a prompt box and determine if it is a multiple of the number they entered. Keep count of how many multiples of their number they entered. Have the user enter a 0 when they are finished entering numbers. At the end, print out the number of multiples of their number they entered.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





