Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:27:31+00:00 2026-06-07T19:27:31+00:00

I’m just wondering how i could make this code more efficient. I feel like

  • 0

I’m just wondering how i could make this code more efficient. I feel like there is a lot of code here to accomplish a simple task. But i’m not sure how to better it? Is there a way to dynamically create divs? Or is tehre a way to reduce the number of “getElementById” calls i make?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:27:32+00:00Added an answer on June 7, 2026 at 7:27 pm

    There are a number of improvements you might make, whether it’s in terms of speed, readability, or maintainability. Here’s a list of some off the top of my head:

    1. Cache the note element in a variable if you’re going to reuse it. This makes your code faster since you don’t have to grab the element again to use it.

      var noteDiv = document.getElementById("A");
      noteDiv.note = notes[9];
      noteDiv.onmouseover = displayIt;
      
    2. Instead of repeating your code so much, you could iterate over something that keeps track of your notes. In this example I am using an array of ids and an object containing the ids and notes as property-value pairs.

      var noteDivs = ["Middle", "D", "E", "F", ... ]
      // note that the order you put notes in here does not matter
      var notes = { Middle: "Middle C",
                    D: "D",
                    E: "E",
                    F: "F",
                    ... }
      function MakingNoise () {
          for (var i = 0; i < noteDivs.length ; i++) {
              document.getElementById(noteDivs[i]).note = notes[noteDivs[i]];
              document.getElementById(noteDivs[i]).onmouseover = displayIt;
          }
      }
      
    3. The box element can be cached as well through a global variable. Might not be good style, but this should be faster than getting it from the DOM each time.

      var screen=document.getElementById("box"); 
      function displayIt() {
          if (this.note && screen) {
              screen.innerHTML += note + "  ";
          }
      } 
      

    Putting everything together, you have the following:

    var screen=document.getElementById("box"); 
    var noteDivs = ["Middle", "D", "E", "F", ... ]
    var notes = { Middle: "Middle C",
                  D: "D",
                  E: "E",
                  F: "F",
                  ... }
    
    function MakingNoise () {
        for (var i = 0; i < noteDivs.length ; i++) {
            var noteDiv = document.getElementById(noteDivs[i]);
            noteDiv.note = notes[noteDivs[i]];
            noteDiv.onmouseover = displayIt;
        }
    }
    function displayIt() {
        if (this.note && screen) {
            screen.innerHTML += note + "  ";
        }
    } 
    

    It might even be better to keep your notes array instead of using the notes object I have above, as long as you make sure the order is the same as the noteDivs array. It will be faster, but also slightly more work to maintain. You would also need to have one div for each element in notes, which you currently don’t have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.