Asked by Anonymous
how do i print this with javascript with variables Men's Basketball Winners:
Gold: United States of America
Silver: Serbia
Bronze: Spain
Women's Basketball Winners:
Gold: United States of America
Silver: Spain
Bronze: Serbia
All Answers 1
Answered by
GPT-5 mini
AI
Here are a few simple ways to print that text in JavaScript. Use the one that fits your environment (console or browser).
1) Console (Node.js or browser console) — using template literals and variables:
```js
const mens = {
gold: 'United States of America',
silver: 'Serbia',
bronze: 'Spain'
};
const womens = {
gold: 'United States of America',
silver: 'Spain',
bronze: 'Serbia'
};
console.log(`Men's Basketball Winners:
\tGold: ${mens.gold}
\tSilver: ${mens.silver}
\tBronze: ${mens.bronze}
Women's Basketball Winners:
\tGold: ${womens.gold}
\tSilver: ${womens.silver}
\tBronze: ${womens.bronze}`);
```
2) Browser DOM — insert as preformatted text (preserves newlines/tabs):
```html
<!-- put this in an HTML page or run in the browser console -->
<script>
const mens = { gold: 'United States of America', silver: 'Serbia', bronze: 'Spain' };
const womens = { gold: 'United States of America', silver: 'Spain', bronze: 'Serbia' };
const pre = document.createElement('pre');
pre.textContent = `Men's Basketball Winners:
\tGold: ${mens.gold}
\tSilver: ${mens.silver}
\tBronze: ${mens.bronze}
Women's Basketball Winners:
\tGold: ${womens.gold}
\tSilver: ${womens.silver}
\tBronze: ${womens.bronze}`;
document.body.appendChild(pre);
</script>
```
3) Browser DOM — build semantic HTML (headings + lists):
```js
function makeSection(title, winners) {
const section = document.createElement('section');
const h = document.createElement('h2');
h.textContent = title;
section.appendChild(h);
const ul = document.createElement('ul');
['gold','silver','bronze'].forEach(medal => {
const li = document.createElement('li');
li.textContent = `${medal[0].toUpperCase() + medal.slice(1)}: ${winners[medal]}`;
ul.appendChild(li);
});
section.appendChild(ul);
return section;
}
const mens = { gold: 'United States of America', silver: 'Serbia', bronze: 'Spain' };
const womens = { gold: 'United States of America', silver: 'Spain', bronze: 'Serbia' };
document.body.appendChild(makeSection("Men's Basketball Winners:", mens));
document.body.appendChild(makeSection("Women's Basketball Winners:", womens));
```
Notes:
- Template literals (backticks) support multiline strings and ${...} interpolation.
- Use textContent or innerText to avoid inserting raw HTML when you want plain text.
1) Console (Node.js or browser console) — using template literals and variables:
```js
const mens = {
gold: 'United States of America',
silver: 'Serbia',
bronze: 'Spain'
};
const womens = {
gold: 'United States of America',
silver: 'Spain',
bronze: 'Serbia'
};
console.log(`Men's Basketball Winners:
\tGold: ${mens.gold}
\tSilver: ${mens.silver}
\tBronze: ${mens.bronze}
Women's Basketball Winners:
\tGold: ${womens.gold}
\tSilver: ${womens.silver}
\tBronze: ${womens.bronze}`);
```
2) Browser DOM — insert as preformatted text (preserves newlines/tabs):
```html
<!-- put this in an HTML page or run in the browser console -->
<script>
const mens = { gold: 'United States of America', silver: 'Serbia', bronze: 'Spain' };
const womens = { gold: 'United States of America', silver: 'Spain', bronze: 'Serbia' };
const pre = document.createElement('pre');
pre.textContent = `Men's Basketball Winners:
\tGold: ${mens.gold}
\tSilver: ${mens.silver}
\tBronze: ${mens.bronze}
Women's Basketball Winners:
\tGold: ${womens.gold}
\tSilver: ${womens.silver}
\tBronze: ${womens.bronze}`;
document.body.appendChild(pre);
</script>
```
3) Browser DOM — build semantic HTML (headings + lists):
```js
function makeSection(title, winners) {
const section = document.createElement('section');
const h = document.createElement('h2');
h.textContent = title;
section.appendChild(h);
const ul = document.createElement('ul');
['gold','silver','bronze'].forEach(medal => {
const li = document.createElement('li');
li.textContent = `${medal[0].toUpperCase() + medal.slice(1)}: ${winners[medal]}`;
ul.appendChild(li);
});
section.appendChild(ul);
return section;
}
const mens = { gold: 'United States of America', silver: 'Serbia', bronze: 'Spain' };
const womens = { gold: 'United States of America', silver: 'Spain', bronze: 'Serbia' };
document.body.appendChild(makeSection("Men's Basketball Winners:", mens));
document.body.appendChild(makeSection("Women's Basketball Winners:", womens));
```
Notes:
- Template literals (backticks) support multiline strings and ${...} interpolation.
- Use textContent or innerText to avoid inserting raw HTML when you want plain text.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.