如何在条件下使用即时响应
How to use prompt response in condition
我对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!");
}
工作小提琴。
您必须首先将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!");
}
希望这能有所帮助。
将响应保存到其他应答状态的变量中。
然后你可以让然后调用一个函数来找到答案。
var name = prompt("who's information do you need? Zack,Steve, or Bill's)
function logAddress(person){
switch(person){
case 'Zack':
console.log(retrieveState(Zack) + retrieveCityState(Zack));
break;
case 'Steve':
console.log(retrieveState(Steve) + retrieveCityState(Steve));
break;
case 'Bill':
console.log(retrieveState(Bill) + retrieveCityState(Bill));
break;
Default:
console.log('sorry person not found');
}
}
logAddress(name);
希望对有所帮助
相关文章:
- html+javascript:如何在某些条件下禁用dropdownlist中的null选项
- 在特定条件下从存储在localStorage中的阵列中删除对象
- 如何在一定条件下使必填字段为真
- 如何在特定条件下完全禁用FullPage.js
- 在条件下使用 slideUp() 和 slideDown()
- 停止计算以在特定条件下更新
- 如何加载特定天气条件下的图标?(AngularJS)
- 比较IF条件下的两个javascript数值
- ComboBox typeAhead有效,但在某些行为条件下valueField为空
- 返回三元条件下的新promise Q
- jqgrid在条件下动态设置单元格可编辑false
- innerHTML在else条件下未更改
- 如何在两种条件下显示相同的上下文菜单项
- Regex用于在特定条件下为新句子添加空格或句号
- HTML页面在使用JavaScript的条件下重新加载
- 保持函数在一定条件下执行
- 在不同条件下显示变量的不同值
- 我如何才能在某些条件下更改c#中谷歌地图标记的图标
- 如何在条件下使用即时响应
- 迭代Ajax响应并在某些条件下创建动态表