【问题标题】:JavaScript function not being recognized as a function using prompt/alertJavaScript 函数未被识别为使用提示/警报的函数
【发布时间】: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


    【解决方案1】:

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

    在我的 sn-p 中,我已将 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)

    【讨论】:

    • 工作就像一个魅力谢谢你,感谢提示/警报冗余的提醒,这是我需要忘记的东西
    【解决方案2】:

     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)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 2013-01-12
      相关资源
      最近更新 更多