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

  • SEARCH
  • Home
  • 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 6231187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:53:15+00:00 2026-05-24T09:53:15+00:00

What is the minimum change to the last 3 lines of code that would

  • 0

What is the minimum change to the last 3 lines of code that would make it work without adding or removing any lines of code?

<script>
function produceMessage(){
    var msg= 'This should print';
    return msg;
}

</script>

<div id="myDiv"> This should not print!</div>

<script>
    var element=document.getElementById('myDiv').innerHTML;
    var message= produceMessage();
    element=message;</script>

For example this is not what I’m looking for since it combines the last three lines of code into one:

document.getElementById('myDiv').innerHTML= produceMessage();
  • 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-05-24T09:53:15+00:00Added an answer on May 24, 2026 at 9:53 am

    from

    <script>
        var element=document.getElementById('myDiv').innerHTML;
        var message=produceMessage();
        element=message;</script>
    

    to

    <script>
        var element=document.getElementById('myDiv');
        var message=produceMessage();
        element.innerHTML=message;</script>
    

    .


    The reason why your first one doesn’t work is because you are assigning the text from the myDiv element to var element. Strings are primitive, so they are passed by value, meaning that a copy of the text from .innerHTML is copied into var element.

    Rather than that, we want a reference to the HTML Element. Any non-primitive types are passed by reference by default in Javascript. To do that, we use

    var element=document.getElementById('myDiv');
    

    Now, var element points to the same object as the document.getElementById('myDiv'), rather than a different instance of the string from its innerHTML, meaning that when we modify the element.innerHTML, we also modify the same object as document.getElementById('myDiv').innerHTML, which happens to be the one on the DOM (web page), rather than a new and different copy of the string.

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

Sidebar

Related Questions

What are the basic minimum set of Code Analysis rules that you would recommend
Using PHP , I would like to make a while loop that reads a
I want to make a single event that will capture every change in the
I wrote a simple coin change algorithm that currently finds the minimum number of
I'd like to change the minimum for the id of created objects from 1
What is the minimum set of HTTP verbs that a server should allow for
What is the minimum code required to read a file and assign its contents
I am trying to write a script that will look through a set of
Basically I need to do some calculations when the Maximum or Minimum properties change,
I'm having trouble figuring out my last section of code for a Dynamic Coin

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.