如何在条件下使用快速响应

时间:2015-11-08 13:36:02

标签: javascript

我对JavaScript非常陌生并致力于掌握它。

我用一堆个人信息属性创建了一些对象。我试图这样做,以便当网站提示用户要求三个名字之一时,在输入任一名称时,吐出该人所在的位置。希望有道理。这是代码。记住,很多这只是我练习我所知道的。有些事情似乎不应该存在,但是它就是它的本质。

那么我如何根据用户的反应来填补我的条件呢?

JS:

//Prompting who's information is needed//
prompt("Who's information do you need? Zack, Steve, or Bill's?");

//Objects here//
var Zack = {
    firstname: 'Zack', 
    lastname: 'Doe',
    address: {
        street: '5555 Timberwood Lane',
        city: 'Hotel',
        state: 'OH'
    }
};

var Steve = {
  firstname: 'Steve',
  lastname: 'Jobs',
  address: {
    street: '1234 Apple Way',
    city: 'Appledom',
    state: 'CA'
    }
};

var Bill = {
  firstname: 'Bill',
  lastname: 'Gates',
  address: {
    street: '5678 Microsoft Ave',
    city: 'Windows',
    state: 'FL'
  }
};

//Just a basic function, input name log Hi + name. Just practice. 
function greet(person){
    console.log('Hi ' + person.firstname);
}

//Functions used to retrieve information from objets Zack, Steve, Bill
function retrieveState(person) {
    console.log (person.firstname + ' is from ' + person.address.street);
}
function retrieveCityState(person) {
  console.log(' and resides in ' + person.address.city + ', ' + person.address.state);
}

//I want to make it so if I enter either, Zack, Bill, or Steve, into the prompt, it will print their information
if () {
  console.log(retrieveState(Zack) + retrieveCityState(Zack));
}
else if () {
  console.log(retrieveState(Steve) + retrieveCityState(Steve));
}
else if () {
  console.log(retrieveState(Bill) + retrieveCityState(Bill));
}
else {
  console.log("That person is unavailable!");
}

2 个答案:

答案 0 :(得分:3)

<强> Working fiddle

首先要将prompt中填写的用户响应保存到变量中:

var name = prompt("Who's information do you need? Zack, Steve, or Bill's?");

之后,您可以使用此name来制定条件。

if ( name == 'Zack' ) {
    console.log(retrieveState(Zack) + retrieveCityState(Zack));
}else if ( name == 'Bill' ) {
    .....

完整代码:

//Prompting who's information is needed//
var name = prompt("Who's information do you need? Zack, Steve, or Bill's?");

//Objects here//
var Zack = {
    firstname: 'Zack', 
    lastname: 'Doe',
    address: {
        street: '5555 Timberwood Lane',
        city: 'Hotel',
        state: 'OH'
    }
};

var Steve = {
  firstname: 'Steve',
  lastname: 'Jobs',
  address: {
    street: '1234 Apple Way',
    city: 'Appledom',
    state: 'CA'
    }
};

var Bill = {
  firstname: 'Bill',
  lastname: 'Gates',
  address: {
    street: '5678 Microsoft Ave',
    city: 'Windows',
    state: 'FL'
  }
};

//Just a basic function, input name log Hi + name. Just practice. 
function greet(person){
    console.log('Hi ' + person.firstname);
}

//Functions used to retrieve information from objets Zack, Steve, Bill
function retrieveState(person) {
    console.log (person.firstname + ' is from ' + person.address.street);
}
function retrieveCityState(person) {
  console.log(' and resides in ' + person.address.city + ', ' + person.address.state);
}

//I want to make it so if I enter either, Zack, Bill, or Steve, into the prompt, it will print their information
if ( name == 'Zack') {
  console.log(retrieveState(Zack) + retrieveCityState(Zack));
}
else if ( name == 'Steve') {
  console.log(retrieveState(Steve) + retrieveCityState(Steve));
}
else if ( name == 'Bill') {
  console.log(retrieveState(Bill) + retrieveCityState(Bill));
}
else {
  console.log("That person is unavailable!");
}

希望这有帮助。

答案 1 :(得分:0)

将响应保存到变量中,如其他答案所示。

然后你可以调用一个函数来找到答案。

document.addEventListener('deviceready', function () {/* your code goes here */});

希望有所帮助