(非常)新手Javascripter需要帮助理解实际功能

(非常)新手Javascripter需要帮助理解实际功能,javascript,function,Javascript,Function,因此,我正在制定一个选举脚本,将候选人分配给政党和选民,然后分配他们的选票,但我非常紧张,如果有人能帮助我了解我在做什么,例如,这里的功能: class Electorate { constructor (newNames) { this.name = newNames this.allMyElectorates = [] this.addWinner = [] this.addPartyVotes = [] } addElectorate (...newN

因此,我正在制定一个选举脚本,将候选人分配给政党和选民,然后分配他们的选票,但我非常紧张,如果有人能帮助我了解我在做什么,例如,这里的功能:

        class Electorate    {
constructor (newNames) {
this.name = newNames
this.allMyElectorates = []
this.addWinner = []
this.addPartyVotes = []
    }
    addElectorate (...newNames) {
        for(let i=0;i<newNames.length;i++){
            var newElectorate = new Electorate(newNames[i], this.addWinner, 
            this.addPartyVotes)
            this.allMyElectorates.push(newElectorate)
            return newElectorate
    }
}
    addWinner(...newNames){
        theParty = myElection.findParty (partyName)
            if (theParty == undefined){
                myElection.addIndependant (cadidateName)
            }else{
                theCandidate = theParty.findCandidate (candidateName)
            }if (theCandidate == undefined) {
                theParty.addElectorateMP  (candidateName, this)
            }else{
                theCandidate.setElectorate (this)}
        }
    function totalVotes(...newNumbers){
        for(let i=0;i<newNumbers.length;i++){
            var newTotalVotes = new totalVotes(newNumbers[i], this)
            this.addPartyVotes.push(newTotalVotes)
            return newTotalVotes
        }
    }
    function addWinner(...newNames){
        for(let i=0;i<newNumbers.length;i++){
            var addWinner = new Winner(newNames[i], this)
            this.addWinner.push(newWinner)
            return addWinner
        }
    }


}
我想使用从addPartyVotes(在控制器类中)收集的数据创建一个新函数(totalVotes),该数据必须从其他类调用,它有它的变量,我在数组中推出它,然后返回它,那么我做错了什么


我试着问我班上的人和导师,但我觉得他们只是在骗我,没有给我任何真正的指导,我是工程师而不是程序员,所以我很难理解这一点

函数不能将
作为参数

有效函数如下所示:

function totalVotes ( vote ) {
   return "something";
}

如果您可以分享与投票/选举程序相关的全部脚本,我可以帮助您和您编写有效代码的方法。

没有哪一行或哪一点代码会破坏您的程序。 错误数不胜数,根本无法正常工作

看看这个例子(JSES5):

var选民={
候选人:[],
init:function(){
此参数为:[];
归还这个;
},
getCandidateByName:函数(名称){
var filter_candidate_by_name=this.candidates.filter(函数(d){
返回d.name==name;
})[0];
var index_of_candidate=this.candidates.indexOf(按_名称筛选_candidate_);
返回此.candidates[索引_of_candidate];
},
calculateWinner:函数(){
var max_vots=Math.max.apply(Math,this.candidates.map)(函数(d){
返回d.length;
}));
如果(!max|u voces | isNaN(max|u voces))返回false;
var记录的票数=this.candidates.filter(函数(d){
返回d.voces.length==最大票数;
});
var result={};
如果(记录的最大投票数长度大于1){
result.result=‘平局’;
result.candidates=记录最多投票数;
var list\u of_candidate\u names=记录_与_max\u voces.map(函数(d){
返回d.name;
})。加入(‘,’);
result.explainion='并列'+候选人名单'+',计数为'+最大票数+'各票数';
}else if(用最大票数记录票数。长度==1){
result.result='Won';
result.candidate=记录了最多的选票[0];
result.explaination=记录最多投票数为[0]的投票数。name+“获胜”,计数为“+max\u投票数+“投票数”;
}
返回结果;
}
};
var表决器={
名称:空,
年龄:空,
性别:空,
init:函数(姓名、年龄、性别){
如果(!name | | |!age | |!gender)返回false;
this.name=名称;
这个。年龄=年龄;
这个。性别=性别;
归还这个;
}
};
变量候选={
名称:空,
表决:[],
init:函数(名称){
this.name=名称;
这是投票数=[];
归还这个;
},
castVote:功能(投票者){
这个。投票。推(选民);
归还这个;
}
};
var electorate=Object.create(electorate.init();
选民、候选人、推举(
Object.create(Candidate.init('Mr.John Smith'),
Object.create(Candidate.init('Mr.Adam John'),
Object.create(Candidate.init('Ms.Christina Brown'))
);
选民
.getCandidateByName(“约翰·史密斯先生”)
.castVote(Object.create(Voter.init('Maria Smith',38,'Female'))
.castVote(Object.create(Voter.init('johndoe',118,'Male'))
.castVote(Object.create(Voter.init('johndoe',44,'Male'));
选民
.getCandidateByName(“克里斯蒂娜·布朗女士”)
.castVote(Object.create(Voter.init('johndoe',235,'Male'))
.castVote(Object.create(Voter.init('johndoe',34,'Male'))
.castVote(Object.create(Voter.init('johndoe',22,'Male'));

console.log(electorate.calculateWinner())我不知道
I
在哪里定义了
newNumbers[I]
请包括所有相关的源代码。如果你是一名工程师,那么了解编程就不难了。任何语言的编程都是应用逻辑来解决问题。作为工程师,你一定见过应用数学编程也有类似的特点。所以不要害怕,多读一点关于
JavaScript
的内容,多想想。编程是95%的思考和5%的编码。另外,如果你在代码中多描述一些代码,并在代码中添加注释,也会对我有帮助。当我说“工程师”时,我的意思是我擅长构建东西,数学对我来说也可能是俄语,但你的权利,我只需要找到正确的方式,让我学习学校希望我学习的东西。干杯,我可以在整个“选民”课程中编辑,我现在正在尝试的一个,但是有几个类文件,所以不确定如何添加它们。你能复制/粘贴这些文件的内容吗?另外,整个功能也不会像你想象的那样工作。谢谢你的回复,我做了代码学院网站,认为我有一个合理的理解,但后来我们得到了这个任务,它完全不同,布局和几乎所有的东西,我的心完全被炸开了。所以我会凭直觉/google/cut/paste,虽然google帮不了什么忙,因为我找不到导师给我们的格式。我只是有点困惑和害怕。很高兴我能帮上忙。
function totalVotes ( vote ) {
   return "something";
}