user3658616
user3658616

Reputation: 33

console.log(); that goes in <p>?

I have a little program where you have a list of ice cream flavors and the computer use "console.log();" to print a sentence and the flavour. I have started the project and it looks like this:

var randomFlavour = Math.random() * 10;

    if (randomFlavour < 1) {
    var randomFlavour = "chocolate";
} else if (randomFlavour < 2) {
    var randomFlavour = "vanilla";
} else if (randomFlavour < 3) {
    var randomFlavour = "pistachio";
} else if (randomFlavour < 4) {
    var randomFlavour = "strawberry";
} else if (randomFlavour < 5) {
    var randomFlavour = "cotton candy";
} else if (randomFlavour < 6) {
    var randomFlavour = "cookie dough";
} else if (randomFlavour < 7) {
    var randomFlavour = "bubblegum";
} else if (randomFlavour < 8) {
    var randomFlavour = "peanut butter";
} else if (randomFlavour < 9) {
    var randomFlavour = "mint";
} else {
    var randomFlavour = "gingerbread man";
}

console.log("Hello. I would like to have" + " " + randomFlavour + " " + "ice cream please.");

The problem is; this will just print in the console, and I want the text printed to transform in <p> paragraphs, is it possible?

Upvotes: 0

Views: 816

Answers (4)

potashin
potashin

Reputation: 44591

Create <p></p> element, edit it's innerHTML, add to the DOM :

var par = document.createElement('p')
par.innerHTML = "Hello. I would like to have " + randomFlavour + " ice cream please."
document.body.appendChild(par)

Upvotes: 1

Justin Lessard
Justin Lessard

Reputation: 11941

First, you need to access your <p> element.

You will need to asssign an id to your element:

<p id="myPar">
</p>

Then you can get in in js like so:

var par = document.getElementById('myPar');

Then to set its text, you can use innerHTML

par.innerHTML  = randomFlavour;

Upvotes: 1

Squiznab
Squiznab

Reputation: 11

Instead of console.log(); you could use,

document.getElementById("idhere").innerHTML = "your string here";

for "idhere" put the id of the paragraph you want to change.

Upvotes: 1

Shan Robertson
Shan Robertson

Reputation: 2742

On the same level you wrote the console.log() do a document.write(); with the output. console.log only outputs to the console whereas document.write writes to your html page.

Upvotes: -1

Related Questions