create undirected lists using an array

I am trying to convert an array of sentences into unordered HTML lists, with each list item containing a word from the sentence. For example: [I play piano the can'] t

<ul>
<li id = number</li>
<li>I</li>
<li>play</li>
<li>piano</li>
<li>the</li>
<li>can</li>
</ul> 

However, when I use this code to iterate through the array:

    function makeQuest() {
      var quest=['I play piano the can', 'tired I am', 'are seven There week in a days'];
     
      for (var i=0; i< quest.length; i++){
            document.write('<ul class ="div3">')
       document.write('<li id = "number">' + (i + 1) + '.' + ' '+ '</li>')
      for (var j=0; j < quest[i].length; j++){
      document.write('<li>')
      document.write(quest[i][j]) 
      document.write('</li>' + '</ul>')
      }
      }
     };
     makeQuest()

The output is not as expected:

1.I
play piano the can
2. t
ired I am
3. a
re seven There week in a days.

I need help identifying what I am doing wrong in my script.

Answer №1

separate the strings based on spaces (your method operates on characters instead of words):

function createQuest() {
  var quest=['I play piano the can', 'tired I am', 'are seven There week in a days'];

  for (var i=0; i< quest.length; i++){
    document.write('<ul class ="div3">')
    document.write('<li>' + (i + 1) + '. </li>')
    for (var j=0; j < quest[i].split(' ').length; j++){
      document.write('<li>')
      document.write(quest[i].split(' ')[j]) 
      document.write('</li>')
    }
    document.write('</ul>')
  }
};
createQuest()

Also (this falls outside the scope of your issue) avoid using id="number" more than once.

Learn more about split()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"I am looking to use JavaScript to transform Latitude and Longitude coordinates into a UK postcode. How can

I'm currently working on a project that involves utilizing HTML5 geolocation. Getting the latitude and longitude of a user through geolocation seems to be fairly straightforward. However, my challenge lies in converting this data into a UK postcode ...

What is the best way to display multiple components using unique component names?

I have a variety of components, each with the same parameter but different iterative values: import React from "react"; import Box from "./Box"; import Header from "./Header"; export default function App() { return ( &l ...

Obtaining metadata from Youtube videos with Javascript and HTML5

Is there a way to fetch YouTube video data with Javascript and HTML5? If so, could you provide some helpful resources or links? ...

Basic Block - Three.JS

My goal was to create a simple rotating 3D cube using the Three.js library for WebGL. Despite following numerous tutorials, I can't figure out why my code only displays a black screen with no geometry. //Setting window dimensions var width = windo ...

Is there a way to trigger a JavaScript alert to notify whether a record already exists or not within a PDO MySQL insert query?

I am working with a MySQL database and looking to insert data from a PHP form using PDO connect. Can anyone help me figure out how to display a JavaScript alert popup in the following scenarios: When a record already exists. When a new record has been ad ...

What is the best way to set the first option in a mat-select to be

My approach to date selection involves using 3 mat-select components for day, month, and year. You can view a demo of this setup here. In an attempt to improve the code, I decided to set the initial options as null by modifying the following lines: allDat ...

What steps can I take to ensure my website is optimized for a seamless viewing experience on all mobile devices

Could you please check out the link on your smartphone and let me know if it looks a bit off to you? I have centered everything and the website looks fine on desktop, but on my iPhone 5S, it doesn't seem to display properly. Is there a way to make it ...

Leveraging a third-party library in AngularJS to interact with a factory or service

My current challenge involves developing an application that is predominantly angular-based, but also incorporates some non-angular code. I have structured my code into modules, including various factories. One of these factories is specifically dedicated ...

I am encountering a JSON parsing error while trying to implement jQuery autocomplete, despite using a local array

I'm attempting to implement the jQuery autocomplete feature on a WordPress website. My ultimate goal is to link the input field to an ajax request that will retrieve data from a database. However, I've encountered an unusual error when trying to ...

The Material UI Icon is missing from the location '@mui/icons-material/Send.js'

I am currently utilizing the Material UI library and attempting to import SendIcon through this import statement: import { SendIcon } from "@mui/icons-material/Send.js"; Due to including "type" : "module" in my package.json f ...

Retain the Previously Selected Option in Angular 2 Dropdown

Looking for assistance with implementing a simple HTML select dropdown in Angular2 (TS) using the code below: <select id="pageSize" (change)="onPageSizeChanged($event, pagination.pageSize)"> <option value="10">10</option> <option ...

Guide to successfully passing a function as a prop to a child component and invoking it within Vue

Is it really not recommended to pass a function as a prop to a child component in Vue? If I were to attempt this, how could I achieve it? Here is my current approach: Within my child component - <template> <b-card :style="{'overflow-y&apo ...

Implement isotope filter on product page details using ASP.NET Core MVC

While working on my .Net core project, I implemented an isotope filter dynamically. Everything seemed to be functioning correctly, however, a minor error occurred. When selecting a specific category, the position of the product did not appear as expected. ...

How can I retrieve the index of an element within a 2D array using JavaScript when the array is displayed on a webpage?

https://i.stack.imgur.com/GHx9p.png I am currently working on developing an Othello game board within an HTML page using Angular. The board itself is constructed from a 2D number array which I then display using the <table> tag on the webpage. Below ...

Tips for shortening extra text in a non-wrapping HTML table cell and adding "..." at the end

My HTML template includes text imported from a database field into a <td> tag. The length of the text can range from 3 to 200 characters, and the <td> spans 100% of the screen width. If the text surpasses the width of the screen, I want it to b ...

Determining Whether a Specific Value Exists in the JQuery Selector '.name'

Is there a way to check if a specific value exists within the elements with the class of $('.name')? I'm already looping through an array to look for each entry in $('.name'), so I can't iterate over $('.name') as we ...

Cursor-hugging tooltip

My tooltip creation works when hovering over an icon, but there's a slight issue - it doesn't always follow the cursor and can get stuck at times. To see a demo of this in action, click here: fiddle Here's the code I'm using: HTML & ...

Wait to execute until the external script in Vue.js has finished loading

How can I trigger the rendering of a recaptcha after a Vue.js component has mounted? It functions correctly on initial load and reload, but throws an error when navigating away to a different url and using the browser back button. Here is how it's se ...

Enhance the appearance of a multidimensional array using main and sub-main styling

Struggling with a new challenge here... after spending hours trying to wrap my head around it, I'm still stuck. Let's dive in... I've worked with arrays before, but only simple ones like this: $arr = array('1', '2', &apo ...

Managing JSON responses from a server using Javascript

I have encountered various similar issues, but none of them have provided a solution for my specific question. On my server, I generate a JSON string and place it in the response: List<String> list = getSomeList(); JSONArray jsArray = new JSONArray( ...