簡體   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