带有替换变量的多行字符串Javascript(Madlibs)

问题描述 投票:0回答:1

我无法在JavaScript中将我的最后一个变量作为警报显示。我正在创建一个Madlibs故事,需要将其打印为用Java警告

这里是我到目前为止的代码:

var story = `Justin is a normal microbiologist. Then, one day, a 
    virus exploded, 
    causing a pandemic to blow up around the world and everybody went 
    crazy. The world went into quarantine and Justin's black and white 
    dog 
    became incredibly bored. Justin decided to take the dog out for a 
    run. During the run the dog tripped Justin and Justin broke his arm. 
    Justin had to walk to the old hospital with the dog because nobody
    was there to help.
    The end.`;

alert(story)
var name = prompt("Enter a persons name : ")
var occupation = prompt("Enter an occupation: ")
var noun1 = prompt("Enter any animal: ")
var noun2 = prompt("Enter a body part: ")
var verb1 = prompt("Enter a verb: ")
var verb2 = prompt("Enter another verb: ")
var adj2 = prompt("Enter an adjective for the animal: ")
var adj1 = prompt("Enter an adjective for the hospital: ")


var newStory = ` ` + name + ` is a normal ` + occupation + `. Then, 
    one day, a virus exploded, 
    causing a pandemic to blow up around the world and everybody went 
    ` + verb + `. The world went into quarantine and ` + name + `s ` +
  adj2 + ` ` + noun1 + `  
    became incredibly ` + verb2 + `. ` + name + ` decided to take the ` +
  noun1 + ` out for a 
    run. During the run the dog tripped ` + name + ` and ` + name + ` 
    broke his ` + noun2 + `. 
    ` + name + ` had to walk to the ` + adj1 + ` hospital with the ` +
  noun1 + ` because nobody
    was there to help.
    The end.`;

alert(newStory)

感谢您的时间和精力!

javascript alert prompt
1个回答
0
投票

这是一个简单的错字。您正在呼叫verb,而您将其定义为verb1

检查您的代码:

var verb1 = prompt("Enter a verb: ")

+ verb + `. The world went into quarantine and ` + name + `s ` +
© www.soinside.com 2019 - 2024. All rights reserved.