Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript (2) JS scripts resulting in the same array (for the first script) showing for both

Hey all,
I'm beginning at JS and was going through some exercises online. I'm attempting to have (2) different arrays show when the appropriate button is clicked. However, the "cities" show for both, as opposed to the "countries" for the second. Any help is appreciated.
JavaScript:
<body>
<! attempting button>
<button onclick="document.getElementById('places').innerHTML = text">Click me for cities!</button>
<p id="places"></p>

<script>
const cities = ["Albany", "Denton", "Asheville", "Charlotte", "Dallas", "Houston"];

let i = 0;
let text = "";
while (cities[i]) {
  text += cities[i] + "<br>";
  i++;
}
</script>

<br>

<button onclick="document.getElementById('countries').innerHTML = text">Click me for countries!</button>
<p id = "countries"></p>

<script>
const countries = ["USA", "Bangladesh", "Sweden", "UAE", "Italy", "India", "Belgium", "Denmark", "Netherlands", "Nepal"];

let text = '';
for (let i = 0; i < countries.length; x++) {
  text += countries[i] + "<br>";
}
</script>

</body>
 
I did not see straight off why it did not work as intended. But I noticed two things:

1) You are using the same variables (i and text) in both pieces of script
2) You are using a different loop for the countries than for the cities.

And sure enough, correcting these things makes it work alright:
HTML:
<body>
<! attempting button>
<button onclick="document.getElementById('places').innerHTML = text1">Click me for cities!</button>
<p id="places"></p>

<script>
const cities = ["Albany", "Denton", "Asheville", "Charlotte", "Dallas", "Houston"];

let i1 = 0;
let text1 = "";
while (cities[i1]) {
  text1 += cities[i1] + "<br>";
  i1++;
}
</script>

<br>

<button onclick="document.getElementById('countries').innerHTML = text2">Click me for countries!</button>
<p id = "countries"></p>

<script>
const countries = ["USA", "Bangladesh", "Sweden", "UAE", "Italy", "India", "Belgium", "Denmark", "Netherlands", "Nepal"];

let i2 = 0;
let text2 = '';
while (countries[i2]) {
  text2 += countries[i2] + "<br>";
  i2++;
}
</script>
</body>
 
Thank you so much for your response; it's much appreciated. I was trying to make a "for" and a "while" script, but have not seen an advantage really yet for either one of them. So, with each new script needs a different i (initial) and text variable, correct?
 
Thank you so much for your response; it's much appreciated. I was trying to make a "for" and a "while" script, but have not seen an advantage really yet for either one of them. So, with each new script needs a different i (initial) and text variable, correct?
To be honest I don't know which one of the 3 changes made it work (different counter, different text, same logic in loop). I'd need to to study this a bit more to find out.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom