OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

JSON Object Not Updating Inside FOR Loop

  • Thread starter Thread starter Zackattack
  • Start date Start date
Z

Zackattack

Guest
Can someone please explain why this JSON object is not being updated? This has been annoying me for more than an hour. When I run the code on this website: https://playcode.io/1919282, the fillcolor is the same every time, however on SO, it is different through every iteration, it appears to vary based on certain website compilers...

Code:
function getRandomColor() {
    var letters = 'BCDEF'.split('');
    var color = '';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * letters.length)];
    }
    return color;
}

// Initial setup
var randomColor = getRandomColor();
var headerJSONstyle = { 
    font: { 
        bold: true, 
        color: { rgb: "000000" }, 
        sz: 13 
    },
    fill: {
        fgColor: { rgb: randomColor }
    }
};

console.log('Initial headerJSONstyle:', headerJSONstyle);

// Update within loop
for (var i = 0; i < 10; i++) {
    headerJSONstyle.fill.fgColor.rgb = getRandomColor();
    console.log(`Iteration ${i}:`, headerJSONstyle);
}

// Final result
console.log('Final headerJSONstyle:', headerJSONstyle);


Code:
function getRandomColor() {
    var letters = 'BCDEF'.split('');
    var color = '';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * letters.length)];
    }
    return color;
}

// Initial setup
var randomColor = getRandomColor();
var headerJSONstyle = { 
    font: { 
        bold: true, 
        color: { rgb: "000000" }, 
        sz: 13 
    },
    fill: {
        fgColor: { rgb: randomColor }
    }
};

console.log('Initial headerJSONstyle:', headerJSONstyle);

// Update within loop
for (var i = 0; i < 10; i++) {
    headerJSONstyle.fill.fgColor.rgb = getRandomColor();
    console.log(`Iteration ${i}:`, headerJSONstyle);
}

// Final result
console.log('Final headerJSONstyle:', headerJSONstyle);

<p>Can someone please explain why this JSON object is not being updated? This has been annoying me for more than an hour. When I run the code on this website: <a href="https://playcode.io/1919282" rel="nofollow noreferrer">https://playcode.io/1919282</a>, the fillcolor is the same every time, however on SO, it is different through every iteration, it appears to vary based on certain website compilers...</p>
<pre><code>function getRandomColor() {
var letters = 'BCDEF'.split('');
var color = '';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * letters.length)];
}
return color;
}

// Initial setup
var randomColor = getRandomColor();
var headerJSONstyle = {
font: {
bold: true,
color: { rgb: "000000" },
sz: 13
},
fill: {
fgColor: { rgb: randomColor }
}
};

console.log('Initial headerJSONstyle:', headerJSONstyle);

// Update within loop
for (var i = 0; i < 10; i++) {
headerJSONstyle.fill.fgColor.rgb = getRandomColor();
console.log(`Iteration ${i}:`, headerJSONstyle);
}

// Final result
console.log('Final headerJSONstyle:', headerJSONstyle);
</code></pre>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function getRandomColor() {
var letters = 'BCDEF'.split('');
var color = '';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * letters.length)];
}
return color;
}

// Initial setup
var randomColor = getRandomColor();
var headerJSONstyle = {
font: {
bold: true,
color: { rgb: "000000" },
sz: 13
},
fill: {
fgColor: { rgb: randomColor }
}
};

console.log('Initial headerJSONstyle:', headerJSONstyle);

// Update within loop
for (var i = 0; i < 10; i++) {
headerJSONstyle.fill.fgColor.rgb = getRandomColor();
console.log(`Iteration ${i}:`, headerJSONstyle);
}

// Final result
console.log('Final headerJSONstyle:', headerJSONstyle);</code></pre>
</div>
</div>
</p>
<pre><code></code></pre>
 

Latest posts

Top