简体   繁体   中英

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.

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. 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.

The function isn't being recognized as a function for some reason. Is there something I am missing or is there a better way to store the else if without a function for this use case?

    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) . This tells JS to run the function using loc as the input.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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