简体   繁体   English

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

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

I am trying to write a program that will generate a short and simple description for real estate properties.我正在尝试编写一个程序,该程序将为房地产属性生成简短而简单的描述。 I have (array) word banks with adjectives, prompts for inputs with specific details (square footage, # of bedrooms etc), two functions with else if statements that will generate a specific sentences, and then a "description" variable that joins these sentences, prompted inputs and word bank adjectives together.我有(数组)带有形容词的词库、带有特定细节的输入提示(平方英尺、卧室数量等)、两个带有 else if 语句的函数,它们将生成特定的句子,然后是一个连接这些句子的“描述”变量,提示输入和词库形容词在一起。

I am having trouble calling the result of the functions in the last description variable.我无法调用最后一个描述变量中的函数结果。 I stored the functions as a variable with the variable it is assessing (location and walkDist) within it.我将函数存储为一个变量,其中包含它正在评估的变量(位置和 walkDist)。 When I run this program it doesn't give me the prompts for these variables and prints out the code of the functions instead of the sentence I want it to.当我运行这个程序时,它没有给我这些变量的提示,而是打印出函数的代码而不是我想要的句子。

When I take location and walkDist out of the functions I get the prompt but the webpage returns an error after I input a 0,1,2.当我从函数中获取位置和 walkDist 时,我得到了提示,但在我输入 0、1、2 后网页返回错误。

The function isn't being recognized as a function for some reason.由于某种原因,function 未被识别为 function。 Is there something I am missing or is there a better way to store the else if without a function for this use case?如果没有 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)

The issue is that although you've defined the functions, but you need to call the functions instead of just referring to them by name.问题是虽然你已经定义了函数,但是你需要调用函数而不是仅仅通过名字来引用它们。 That is, instead of bayBeach you need to have bayBeach(loc) .也就是说,您需要拥有bayBeach(loc)而不是bayBeach This tells JS to run the function using loc as the input.这告诉 JS 使用loc作为输入来运行 function。

In my snippet I've changed location to loc because location is a global name in JS (referring to the page location) and the compiler doesn't want it re-declared.在我的代码片段中,我将location更改为loc因为location是 JS 中的全局名称(指页面位置),并且编译器不希望重新声明它。

Also, you don't need to alert inside a prompt, this just brings up two separate dialog boxes.此外,您不需要在提示内发出警报,这只会打开两个单独的对话框。

 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