JavaScript - Uncaught TypeError: type[totypeIndex] is not defined

After following a tutorial and successfully completing the project, I encountered a JavaScript error saying "Uncaught TypeError: totype[totypeIndex] is undefined". When I tried to log the type of totype[totypeIndex], it initially showed as String, but later changed to undefined.

Here is my code:

const typedSpan = document.querySelector(".typed");
const totype = ["Fun", "Hard", "Love"];
const delayTyping_char = 200;
const delayErasing_text = 150;
const delayTyping_text = 3000;
let totypeIndex = 0;
let charIndex = 0;

function typeText() {
  if (charIndex < totype[totypeIndex].length) {
    typedSpan.textContent += totype[totypeIndex].charAt(charIndex);
    charIndex++;
    setTimeout(typeText, delayTyping_char);
  } else {
    setTimeout(eraseText, delayTyping_text);
  }
}

function eraseText() {
  if (charIndex > 0) {
    typedSpan.textContent = totype[totypeIndex].substring(0, charIndex - 1);
    charIndex = charIndex - 1;
    setTimeout(eraseText, delayErasing_text);
  } else {
    totypeIndex++;
    if (totypeIndex >= totype.length)
      totypeIndex = 0;
    setTimeout(typeText, delayTyping_text);
  }
}

window.onload = function() {
  setTimeout(typeText, delayTyping_text);
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: #111111;
  font-family: monospace;
  color: #ffffff;
  text-align: center;
}

.wrapper {
  height: 100vh;
  width: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.effect-wrapper {
  text-align: center;
  font-weight: normal;
}

.typed {
  font-weight: bold;
  color: #00bdff;
}

.cursor {
  display: inline-block;
  background-color: #b0ff95;
  animation: blinker 800ms infinite;
}

.cursor.typing-true {
  animation: none;
}

@keyframes blinker {
  0% {
    background-color: #b0ff95;
  }
  50% {
    background-color: transparent;
  }
  100% {
    background-color: #b0ff95;
  }
}
<div class="wrapper">
  <h1 class="effect-wrapper">Coding is <span class="typed"></span> <span class="cursor">&nbsp;</span></h1>
</div>

If anyone knows how to fix this error, please let me know. Thank you!

Answer №1

typo is present here

if (totypeIndex >= totype.length) // If totypeIndex is equal to or greater than the number of text to be typed then

you must adjust this statement to be higher or equal, so utilize >= instead of >

const typedSpan = document.querySelector(".typed") // Obtaining the Span of HTML with Class 'typed' for altering the text
const totype = ["Fun", "Hard", "Love"] // Storing all the words to be typed in an array

const delayTyping_char = 200; // Wait time before typing the next character
const delayErasing_text = 150; // Wait time before removing the next character
const delayTyping_text = 3000; // Wait time before erasing everything and typing the next text

let totypeIndex = 0; // Index of the text being typed
let charIndex = 0; // Index of the character being typed

function typeText() {
  if (charIndex < totype[totypeIndex].length) {
    typedSpan.textContent += totype[totypeIndex].charAt(charIndex); // Adding the Character at the index of charIndex to the Span's HTML
    charIndex++; 
    setTimeout(typeText, delayTyping_char); 
  } else {
    setTimeout(eraseText, delayTyping_text); 
  }
}

function eraseText() {
  if (charIndex > 0) {
    typedSpan.textContent = totype[totypeIndex].substring(0, charIndex - 1);
    charIndex = charIndex - 1;
    setTimeout(eraseText, delayErasing_text);
  } else {
    totypeIndex++;
    if (totypeIndex >= totype.length) // If totypeIndex is equal to or greater than the number of text to be typed then
      totypeIndex = 0; 
    setTimeout(typeText, delayTyping_text); 
  }
}

window.onload = function() {
  setTimeout(typeText, delayTyping_text);
} 
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: #111111;
  font-family: monospace;
  color: #ffffff;
  text-align: center;
}
.wrapper {
  height: 100vh;
  width: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}
.effect-wrapper {
  text-align: center;
  font-weight: normal;
}
.typed {
  font-weight: bold;
  color: #00bdff;
}
.cursor {
  display: inline-block;
  background-color: #b0ff95;
  animation: blinker 800ms infinite;
}
.cursor.typing-true {
  animation: none;
}
@keyframes blinker {
  0% {
    background-color: #b0ff95;
  }
  50% {
    background-color: transparent;
  }
  100% {
    background-color: #b0ff95;
  }
}
<div class="wrapper">
  <h1 class="effect-wrapper">Coding is <span class="typed"></span> <span class="cursor">&nbsp;</span></h1>
</div>

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

CSS clearfix doesn't appear to be functioning properly

I've been experimenting with using clearfix instead of the clear both property, but it's not working as expected. Despite following tutorials, the issue persists. How can I troubleshoot this and make it function properly? * { marg ...

When the key property is utilized, the state in react useState is automatically updated; however, for updating without it, the useEffect or a

I am working on a component that looks like this: import React, { useState } from "react"; import { FormControl, TextField } from "@material-ui/core"; interface IProps { text?: string; id: number; onValueChange: (text: stri ...

What is the best way to declare a variable within a VueJS v-if condition without it being visible?

In my code, I have a specific template that is displayed when a certain condition is met using a v-if statement. In this template, I need to set a variable to N/A, but I am struggling with how to accomplish this simple task. <span v-if="se ...

Is there a way to select multiple items using the same xpath in Python Selenium by clicking on them?

I am facing a challenge with my webpage code. The code snippet below presents a radio button setup: <label data-correct="false"> <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion&quo ...

Troubleshooting: Issues with AngularJS Data Binding in HTML Tables

I have been working on code from an online tutorial but unfortunately, it is not functioning properly. I have checked the script, app, and controller but still cannot identify the issue. Any assistance would be greatly appreciated! ...

In-depth CSS Reset for specific tags

Creating a Firefox extension that inserts HTML elements into web pages poses the challenge of ensuring consistent styling across various websites. The CSS rules must be robust enough to override any potential modifications introduced by developers. This ta ...

Dealing with the "TypeError: Cannot read property 'style' of undefined" error in material-ui Transitions: A troubleshooting guide

While attempting to incorporate transitions within my react app, I encountered a recurring error whenever I tried to implement any transition module: "TypeError: Cannot read property 'style' of undefined". (anonymous function) node_modules/ ...

What is the best way to structure a hierarchy in an array or object data using Node.js?

I have the following data arrangement: [{ Country: 'USA', State: 'CA', City: 'LA', District: 'LA-1',Town: 'LA-1,LA-1a,LA_AZ_A',Area: 'Area 51'}, { Country: 'USA', State: 'CA&ap ...

Tips for incorporating multi-lingual email templates into your node.js application

I integrated node email templates into my project to automatically send emails to users based on certain events. Check out the Node Email Templates GitHub page for more information. For sending emails with Node.js, Nodemailer is a great tool. While I wa ...

Issue: parsing error, only 0 bytes out of 4344 have been successfully parsed on Node.js platform

I've been attempting to utilize an upload program to transfer my files. The specific code I'm using is as follows: app.post('/photos',loadUser, function(req, res) { var post = new Post(); req.form.complete(function(err, fields, fil ...

How can I eliminate a class when the scrollTop() position matches a specific div element?

I am trying to implement a script that will allow me to scroll to my navigation and then change the class of the navigation, however, it is not working as expected. $(window).scroll(function() { if ($(window).scrollTop == $('.menu_nav').offs ...

Refreshing the page allows Socket.io to establish multiple connections

I've been working on setting up a chatroom, but I've noticed that each time the page refreshes, more connections are being established. It's interesting because initially only one connection is created when I visit the chat room page. Howeve ...

What is the best way to stack div elements one after the other?

I am a complete beginner in HTML and I have a project for my web design class about a movie. http://jsfiddle.net/Lbo1ftu9/ <div id="footer"> <a href="D:/MOVIE REPORT/akira.htm" title="GO BACK?"><img src="D:/IMAGES/goback.gif"> My code ...

Html elements remain stationary and do not shift positions

I am attempting to customize a web page by repositioning an input text element. Below is the code I'm using: <body> <h1>Shopping List</h1> <input id="search" type="search"> <input id='newItem' type="t ...

Adjust the HTML content prior to displaying it to prevent any flickering

Is there a way to run code before the page is rendered, such as updating dates or converting text to HTML, without causing flickering when reloading multiple times? How can I ensure the code runs as it's drawn instead of waiting until the end to redra ...

Running multiple instances of setTimeout() in JQuery

I'm looking for a way to delay the execution of 10 lines of independent jQuery code with 2 seconds between each line. I initially tried using setTimeout() on each line, but is there a more elegant solution for this scenario? The jQuery DELAY method do ...

Running a node.js project on the client side without using npm for deployment

Looking for a solution to efficiently deploy my nodejs project with frequent updates. The site does not have npm available, so I have to package the node_modules every time, which results in a large file size (80MB) that takes a long time to send via ftp t ...

It appears that my array is not being properly populated by my callback functions

I am encountering an issue with my callback functions. The objective of my code is to send 16 GET requests to a REST API in order to retrieve 16 distinct JSON files. These JSON files are then supposed to be converted into dictionaries representing the foot ...

The addEventListener feature in Bootstrap 5.2.3 seems to be malfunctioning

I have a sample page with a Bootstrap dialog being returned from the server. I want to display it inside a DIV. However, when clicked, I receive this error: Uncaught TypeError: myModal.addEventListener is not a function It points to this line of code: ...

A guide to implementing Vuex in a Laravel and Vue 3 project

Currently, I am working on a Laravel + Vue 3 project and need to incorporate Vuex into the project. In my store.js file, I have configured my Vuex as shown below: import { createApp } from 'vue' import { createStore } from 'vuex' const ...