我有这个例子,我怎么能让它显示正确的答案。 如果每个人都在线,则全局状态为在线状态。 如果有人离开,全球状态就会消失。 如果每个人都处于离线状态 - >脱机。
var group, part1, part2, part3;
group = 'Group Status';
part1 = 'online';
part2 = 'away';
part3 = 'offline';
if( part1 === 'online' || part2 === 'online' || part3 === 'online' ){
group = 'Online';
console.log('All users are online. Group status: ' + group);
}else if( part1 === 'away' || part2 === 'away' || part3 === 'away' ){
group = 'Away';
console.log('One of the users is away. Group status: ' + group);
}else if( part1 === 'offline' || part2 === 'offline' || part3 === 'offline' ){
group = 'Offline';
console.log('One of the users is offline. Group status: ' + group);
}else{
group = 'Not found';
console.log('Status not found. Group status: ' + group);
}
答案:此代码的优先级是在线>离开>脱机。
我替换了它,它按我的意愿工作:离线>离开>在线。
var group, part1, part2, part3;
group = 'Group Status';
part1 = 'online';
part2 = 'away';
part3 = 'away';
if( part1 === 'offline' || part2 === 'offline' || part3 === 'offline' ){
group = 'Offline';
console.log('One of the users is offline. Group status: ' + group);
}else if( part1 === 'away' || part2 === 'away' || part3 === 'away' ){
group = 'Away';
console.log('One of the users is away. Group status: ' + group);
}else if( part1 === 'online' || part2 === 'online' || part3 === 'online' ){
group = 'Online';
console.log('All users are online. Group status: ' + group);
}else{
group = 'Not found';
console.log('Status not found. Group status: ' + group);
}
答案 0 :(得分:0)
以下是我认为你想做的事:http://es6fiddle.net/itfgy2xt/
为组部件使用数组很有帮助,因此您可以使用数组方法,即Array.prototype.every
和Array.prototype.indexOf
。请注意,every
是IE9 +。
every
并且true
返回数组中元素的索引或true
时, indexOf
才返回-1
找不到元素。
var groupStatus = 'Group Status',
parts = [];
parts[0] = 'online';
parts[1] = 'away';
parts[2] = 'offline';
// check if everyone is online
if (parts.every(function (el) { return el === 'online'; })) {
groupStatus = 'Online';
}
// check if at least someone is online or away
else if (parts.indexOf('online') >= 0 || parts.indexOf('away') >= 0) {
groupStatus = 'away';
}
else {
groupStatus = 'offline';
}
console.log('Group status: ' + groupStatus);
答案 1 :(得分:0)
您可以将所有部分移动到一个数组中,并使用Array#some
和Array#every
查看所需状态。
var group = 'Group Status',
parts = ['online', 'away', 'offline'];
if (parts.some(function (a) { return a === 'offline'; })) {
group = 'Offline';
console.log('One of the users is offline. Group status: ' + group);
} else if (parts.some(function (a) { return a === 'away'; })) {
group = 'Away';
console.log('One of the users is away. Group status: ' + group);
} else if (parts.every(function (a) { return a === 'online'; })) {
group = 'Online';
console.log('All users are online. Group status: ' + group);
} else {
group = 'Not found';
console.log('Status not found. Group status: ' + group);
}

ES6
var group = 'Group Status',
parts = ['online', 'away', 'offline'],
check = string => item => item === string;
if (parts.some(check('offline'))) {
group = 'Offline';
console.log('One of the users is offline. Group status: ' + group);
} else if (parts.some(check('away'))) {
group = 'Away';
console.log('One of the users is away. Group status: ' + group);
} else if (parts.every(check('online'))) {
group = 'Online';
console.log('All users are online. Group status: ' + group);
} else {
group = 'Not found';
console.log('Status not found. Group status: ' + group);
}