I need help extracting an array name from a JavaScript array. I think it might be a syntax problem, but I am new to programming

In the process of developing a program where a video plays, featuring a person speaking along with chunks of text appearing in a div as the words are spoken alongside their translation. As part of this project, I'm working on writing a JavaScript function to make this synchronization happen smoothly. Essentially, my goal is to manipulate the visibility of the divs using "setTimeout" so that the corresponding text appears at the right moment.

Here are the three arrays involved:

var invite = ["itimes", "idivs"];  
var itimes = ["0", "642", "950", "2555", "1113"];
var idivs = ["speech_bubble", "i1", "i2", "i3", "i4"];

I've created two functions to achieve the desired effect:

function reveal(){                    
    var textchunk = "invite";
    var divs =  textchunk[1];       
    var div = {Value: divs[x]};           
    var divtoreveal = document.getElementById(div);
    divtoreveal.style.visibility = 'visible';   
}

function timedreveal() {
    var textchunk = "invite";      
    var times = textchunk[0];

    for(var x = 0; x < times.length; x++)
    {
        var wait = {Value: times[x]};
        alert(JSON.stringify(wait, null, 4));
        setTimeout(reveal, wait);
    }
}

Although timedreveal successfully retrieves 'itimes' from the 'invite' array, when I check its length through an alert, instead of getting the length of the 'itimes' array, I receive the length of the word 'itimes'. The first value being fetched is "i" rather than "0". It seems like it's treating 'itimes' as the name of an array rather than as the actual array values under 'times' with 6 elements. I have tried researching and reading various resources but haven't been able to understand how to fix this issue. One suggestion was to encapsulate all three arrays within a superarray, but I am unsure how to access the data in such a scenario. Any advice or guidance would be greatly appreciated.

Thank you, Kate

Answer №1

const showTimes =["0", "642", "950", "2555", "1113"]; //times in milliseconds when text should be revealed 
const divIds = ["speech_bubble", "i1", "i2", "i3", "i4"]; //IDs of divs to be revealed
const revealInfo =[showTimes, divIds];
const revealTimes = revealInfo[0]; // used in timedreveal()
const revealDivs =  revealInfo[1]; // used in reveal()

You may want to structure it like this, as you are adding strings to the array instead of arrays themselves.

Answer №2

Make sure you are using an array instead of a string for the assignment.

var textchunk = "invite" 

should be

var textchunk = invite;

In this scenario

> var textchunk = "invite"
> var divs =  textchunk[1];

textchunk is a string with value "invite" and techchunk[1] will give the letter at position 1, so divs will equal to n

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

Tips for Creating and Verifying JWE in Node.js

I attempted to use the following code in order to create an RSA-OAEP and A128GCM JWE generator and validator. It successfully encrypts claims and generates the JWE, then decrypts it and provides me with the original claims when running on node.js. However, ...

Unveiling the Mystery: Uncovering the Selected Item in Ionic Checkboxes

I am trying to implement a feature in Ionic checkboxes where I can get the selected item when a user checks one or more checkboxes. Specifically, I want to identify which books the user has selected. Below are snippets of my code: <ion-item ng ...

Is there a way to verify if the current event marks the conclusion of the events transmitted to a webhook?

Currently, I am in the process of coding a Google Apps Script web app that will react to events from the Telegram API using a chat bot. The first step was setting up a webhook to collect these events through the Google webhook. Since logger.log is unavaila ...

Analyzing elements within an array to eliminate duplicates (PHP and MySQL)

I have a task involving PHP/MySQL that I am currently working on. At the company where I work (not in programming), we are gathering fax numbers and other data from customers to use in case of operational issues. The array structure is as follows: A ...

Press the button within the table as its name undergoes periodic changes

I am using Python along with Selenium to automate the process of selecting and reserving a room on a website. The site displays a table containing available rooms, and my goal is to locate a specific room and click on the corresponding button within the ta ...

Error encountered: Attempting to write to a location in the dynamic array that is

Currently, I am engaged in a project that heavily involves the use of 2D arrays. One particular function within the project generates a 2D array of integers, while others are responsible for finding the shortest path and similar tasks, all functioning flaw ...

Restrict Vue Router access to specific pages only

Once a user has logged in for the first time, they are directed to the CreateProfile page where they can enter their profile information. I want to restrict access to this page so that if the user manually types the URL into their browser (e.g. www.myproje ...

html form shifting positions based on screen resolution

I have been experimenting with a login screen design for my website recently. I created a form with a fixed position, but as the screen resolution changes, not only does the form's position shift, but also the image moves, causing an unwanted interse ...

Setting an Alias for AVA Tests: A Step-by-Step Guide

I need to set up global aliases in my project without using Webpack or Babel. Currently, I am testing with AVA. The npm package module-alias allows me to define aliases in my package.json file. However, when I try to create a basic example following the d ...

Error: Namespace declaration does not have a type annotation - TypeScript/Babel

After creating my app using the command npx create-react-app --typescript, I encountered an issue with generated code and namespaces causing Babel to throw an error. Here are the steps I took to try and resolve the issue: I ejected the project Updated b ...

TypeError: "Table" has not been declared

This is my first experience with the script editor. I have been given the task of creating a pivot table for Google Sheets using a script. // This script creates a pivot table in Google Sheets function createPivotTable() { var ss = SpreadsheetApp.getAc ...

Learn how to transfer a specified character from one array to another in the C programming language

Consider the following scenario with two arrays: char arr1[10]="12345"; char arr2[4][4]={"0","-1"}; The task is to copy the item at index 1 in arr1 into arr2 at index 1. How can this be achieved? An attempt was made by using the command below: strcpy(ar ...

What is the process for installing a previous release of React?

Wondering how to set up an outdated version of React? Take a look at this snippet from my package.json file: "react": "^18.12.0", ...

Error: Unable to parse JSON field value due to an unexpected OBJECT_START

Currently in my coding project, I am utilizing node and mongoose to update a Watson rank and access the database. My goal is to insert multiple documents into the collection. While I can successfully add a single document, I encounter issues when creating ...

`Node.js: Implementing intricate routes with Express`

Just starting out with Node.js and Express here. I have a question about setting up routes in Express.js. In my project, I need to match the following URL: example.com/?campaign=123&click=123. I've tried using the following condition in the app ...

OroCrm is ensuring that Symfony2 profiler seamlessly updates the footer data without triggering a 404 error page within a popup

As a newcomer to OroCrm, I recently installed and configured it on my DEV environment using the app_dev.php entry point. After setting up OroCrm, I immediately noticed the Symfony2 profiler bar appearing at the bottom of the interface. While this was a he ...

Exploring the power of Vue's v-for directive with nested

I have an array within an array that I want to showcase in a table. However, I am struggling to display my nested array correctly. Here is how my data set looks: [ { "dd":"February", "md":[ { "dag":"2020-02-01" }, { "d ...

How come my customized directive is not taking precedence over the original methods in AngularJS?

Following the guidelines in the Angular Decorator manual (https://docs.angularjs.org/guide/decorators), I attempted to create a directive and apply decoration to it. The directive's purpose is to display the current date and time. I included a (point ...

Determine the number of occurrences of a specific object property within an array consisting of multiple objects

Currently, I am working on developing a lottery system using JavaScript and React to enhance my skills. One of the challenges I am facing is creating a function that can identify the most common bet combination in my betObject array and then list all the b ...

Updating textures dynamically in Three.js

My current project involves creating a user interface where users can change the texture of a selected object by clicking on the desired texture image. However, I am facing a challenge where I can only access and use the last texture added in the array. ...