我对JavaScript很新。 我需要两件事:
将这些变量声明为全局只是不起作用,因为我甚至不想在以后更改它们的值。
在其他对象的 hasMoreOscarsThan 方法中使用这些变量时,是否有任何方法可以干掉这些变量?
如您所知,用户大多不会尝试写名称“Leonardo DiCaprio”,例如,大写字母和小写字母。 toLowerCase() 会为我解决问题,但我无法让它发挥作用!为什么?
非常感谢您提前!
var actors = [
leo = {
name: 'Leonardo DiCaprio',
age: 41,
oscars: 1,
hello: function() {
var logActorName0 = console.log('Hello, my name is: ' + leo.name);
// hasMoreOscarsThan - this method accepts one actor object as a parameter and
// returns true if the actor has more Oscars than the one that is passed as
// a parameter and false otherwise.
},
hasMoreOscarsThan: function(actorObj) {
var leoName = actors[0].name;
var jennName = actors[1].name;
var samName = actors[2].name;
var meryName = actors[3].name;
var johnName = actors[4].name;
var leoOscars = actors[0].oscars;
var jennOscars = actors[1].oscars;
var samOscars = actors[2].oscars;
var meryOscars = actors[3].oscars;
var johnOscars = actors[4].oscars;
if (actorObj === leoName && leoOscars > leoOscars) {
return true;
} else if (actorObj === jennName && leoOscars > jennOscars) {
return true;
} else if (actorObj === samName && leoOscars > samOscars) {
return true;
} else if (actorObj === meryName && leoOscars > meryOscars) {
return true;
} else if (actorObj === johnName && leoOscars > johnOscars) {
return true;
} else if (actorObj !== leoName && actorObj !== jennName && actorObj !== samName && actorObj !== meryName && actorObj !== johnName) {
var falseSearch = "Please enter a valid actor name!";
return falseSearch;
} else {
return false;
}
}
},
jenn = {
name: 'Jennifer Lawrence',
age: 25,
oscars: 1,
hello: function() {
var logActorName1 = console.log('Hello, my name is: ' + jenn.name);
},
hasMoreOscarsThan: function(actorObj) {
console.log(leoname);
}
},
sam = {
name: 'Samuel L. Jackson',
age: 67,
oscars: 0,
hello: function() {
var logActorName2 = console.log('Hello, my name is: ' + sam.name);
},
hasMoreOscarsThan: function(actorObj) {
}
},
mery = {
name: 'Meryl Streep',
age: 66,
oscars: 3,
hello: function() {
var logActorName3 = console.log('Hello, my name is: ' + mery.name);
},
hasMoreOscarsThan: function(actorObj) {
}
},
john = {
name: 'John Cho',
age: 43,
oscars: 0,
hello: function() {
var logActorName4 = console.log('Hello, my name is: ' + john.name);
},
hasMoreOscarsThan: function(actorObj) {
}
},
function getActorByName(actor){
var leoName = actors[0].name;
var jennName = actors[1].name;
var samName = actors[2].name;
var meryName = actors[3].name;
var johnName = actors[4].name;
if (actor === leoName){
return (leo);
} else if (actor === jennName){
return (jenn);
} else if (actor === samName){
return (sam);
} else if (actor === meryName){
return (mery);
} else if (actor === johnName){
return (john);
} else if (actor !== leoName && actor !== jennName && actor !== samName && actor !== meryName && actor !== johnName){
return "No Such An Actor Found!";
}
};
<!doctype html>
<html>
<title>Actors</title>
<script src="script.js"></script>
</html>
答案 0 :(得分:1)
我猜您使用单个filter
操作执行此操作,如下所示;
var hasMoreOscars = (o,a) => a.filter(e => e.oscarCount < o.oscarCount),
arr = [{a:"leo",oscarCount:1},{a:"me",oscarCount:10},{a:"John", oscarCount: 3}, {a:"she",oscarCount:7}];
console.log(hasMoreOscars(arr[3],arr))
&#13;
答案 1 :(得分:0)
你想要的只是一个简单的循环:
hasMoreOscarsThan: function (actorObj) {
var myOscars = this.oscars;
for (var i = 0; i < actors.length; i++) {
var actorName = actors[i].name;
if (actorName == actorObj) { // use toLowerCase() here if you want
var actorOscars = actors[i].oscars;
return actorOscars < myOscars;
}
}
return "Please enter a valid actor name!";
}