【发布时间】:2020-10-14 22:33:28
【问题描述】:
我正在尝试编写一个程序,该程序将为房地产属性生成一个简短的描述。我有(数组)带有形容词的词库、带有特定细节的输入提示(平方英尺、卧室数量等)、两个带有 else if 语句的函数,它们将生成特定的句子,然后是一个连接这些句子的“描述”变量, 提示输入和词库形容词在一起。
我无法调用最后一个描述变量中函数的结果。我将函数存储为一个变量,其中包含它正在评估的变量(位置和 walkDist)。当我运行这个程序时,它没有给我这些变量的提示,而是打印出函数的代码而不是我想要的句子。
当我从函数中获取位置和 walkDist 时,我得到提示,但在我输入 0、1、2 后网页返回错误。
由于某种原因,该函数未被识别为函数。如果没有此用例的功能,是否有我遗漏的东西或者是否有更好的方法来存储 else?
let wordBank1 = ['Breathtaking', 'Incredible', 'Beautiful', 'Stunning', 'Incredible', 'Magnificent']
let wb1Selector = wordBank1[Math.floor(Math.random()*wordBank1.length)]
let wordBank2 = ['gorgeous', 'panoramic', 'expansive', 'compelling', 'immaculate']
let wb2Selector = wordBank2[Math.floor(Math.random()*wordBank2.length)]
let sqFoot = prompt(alert('how many square feet?'))
let bedRoom = prompt(alert('how many bedrooms?'))
let bathRoom = prompt(alert('how many bathrooms?'))
let lotSize = prompt(alert('how big is the lot size?'))
// when run program is not recognizing bayBeach and walkingDistance as functions
var location = prompt(alert('if this is on beach block enter a 1, if this is a bayfront enter a 2, if it is neither enter a 0'))
let bayBeach = function(location) {
if (location == 1) {
return 'only a block from the beach, this home boasts of ' + wb2Selector + ' views.'
} else if (location == 2) {
return 'situated on the bay, this property offers ' + wb2Selector + ' views.'
}
}
var walkDist = prompt(alert('if this home is between 90th and 110th street enter a 1, if it is above 40th street enter a 2, otherwise enter a 0'))
let walkingDistance = function(walkDist) {
if (walkDist == 1) {
return 'Enjoy your vacation within walking distance of the pizza joints and novelty shops of downtown Stone Harbor.'
} else if (walkDist == 2) {
return 'Your rental will keep you within a short walk of everything that downtown Avalon has to offer.'
} else {
return 'Get everywhere you need on your vacation with a walk to the beach and a bike ride downtown.'
}
}
let description1 = wb1Selector + ' ' + sqFoot + ' square foot property with ' + bedRoom + ' bedrooms and ' + bathRoom + ' bathrooms. ' + bayBeach + ' '
+ walkingDistance
alert(description1)
【问题讨论】:
标签: javascript function alert prompt