JavaScript 函数未被识别为使用 prompt/alert 的函数
JavaScript function not being recognized as a function using prompt/alert
我正在尝试编写一个程序来生成简短的房地产描述。我有(数组)带有形容词的词库,输入具体细节的提示(平方英尺,卧室数量等),两个带有 else if 语句的函数将生成一个特定的句子,然后是一个连接这些句子的“描述”变量, 提示输入和词库形容词在一起。
我在调用最后一个描述变量中函数的结果时遇到问题。我将函数存储为一个变量,其中包含它正在评估的变量(location 和 walkDist)。当我 运行 这个程序时,它没有给我这些变量的提示,而是打印出函数的代码,而不是我想要的句子。
当我从函数中获取 location 和 walkDist 时,我得到了提示,但网页 returns 在我输入 0,1,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 + ' '
+ walkingDistance
alert(description1)
问题是虽然您已经定义了函数,但您需要调用 函数而不是仅仅通过名称引用它们。也就是说,您需要 bayBeach(loc)
而不是 bayBeach
。这告诉 JS 使用 loc
作为输入 运行 函数。
在我的代码片段中,我将 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)
我正在尝试编写一个程序来生成简短的房地产描述。我有(数组)带有形容词的词库,输入具体细节的提示(平方英尺,卧室数量等),两个带有 else if 语句的函数将生成一个特定的句子,然后是一个连接这些句子的“描述”变量, 提示输入和词库形容词在一起。
我在调用最后一个描述变量中函数的结果时遇到问题。我将函数存储为一个变量,其中包含它正在评估的变量(location 和 walkDist)。当我 运行 这个程序时,它没有给我这些变量的提示,而是打印出函数的代码,而不是我想要的句子。
当我从函数中获取 location 和 walkDist 时,我得到了提示,但网页 returns 在我输入 0,1,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 + ' '
+ walkingDistance
alert(description1)
问题是虽然您已经定义了函数,但您需要调用 函数而不是仅仅通过名称引用它们。也就是说,您需要 bayBeach(loc)
而不是 bayBeach
。这告诉 JS 使用 loc
作为输入 运行 函数。
在我的代码片段中,我将 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)