繁体   English   中英

JavaScript function 未被识别为 function 使用提示/警报

[英]JavaScript function not being recognized as a function using prompt/alert

我正在尝试编写一个程序,该程序将为房地产属性生成简短而简单的描述。 我有(数组)带有形容词的词库、带有特定细节的输入提示(平方英尺、卧室数量等)、两个带有 else if 语句的函数,它们将生成特定的句子,然后是一个连接这些句子的“描述”变量,提示输入和词库形容词在一起。

我无法调用最后一个描述变量中的函数结果。 我将函数存储为一个变量,其中包含它正在评估的变量(位置和 walkDist)。 当我运行这个程序时,它没有给我这些变量的提示,而是打印出函数的代码而不是我想要的句子。

当我从函数中获取位置和 walkDist 时,我得到了提示,但在我输入 0、1、2 后网页返回错误。

由于某种原因,function 未被识别为 function。 如果没有 function 用于此用例,是否有我遗漏的东西或者有更好的方法来存储 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)

问题是虽然你已经定义了函数,但是你需要调用函数而不是仅仅通过名字来引用它们。 也就是说,您需要拥有bayBeach(loc)而不是bayBeach 这告诉 JS 使用loc作为输入来运行 function。

在我的代码片段中,我将location更改为loc因为location是 JS 中的全局名称(指页面位置),并且编译器不希望重新声明它。

此外,您不需要在提示内发出警报,这只会打开两个单独的对话框。

 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('how many square feet?'); let bedRoom = prompt('how many bedrooms?'); let bathRoom = prompt('how many bathrooms?'); let lotSize = prompt('how big is the lot size?'); let loc = prompt('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 walkDist = prompt('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 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.' } } 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(loc) + ' ' + walkingDistance(walkDist) alert(description1)

 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(location) + ' ' + walkingDistance(walkDist) alert(description1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM