2017-09-26 39 views
-2

所以我建立一个选举脚本候选人分配给该党的选民和分配,然后他们自己的选票,但我这么炒,所以如果有人能帮助我理解我在做什么,比如该功能在这里:(非常)新手JavaScript程序员需要帮助理解实用功能

 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 
     } 
    } 


} 

这就是我想要的那一刻引用:

anElectorate = theElection.addElectorate('Auckland Central') 
anElectorate.addWinner('KAYE, Nicola Laura', 'National Party') 
anElectorate.addPartyVotes(329, 85, 10, 486, 3, 2, 6242, 553, 6101, 158, 
          12652, 1459, 7, 17, 53, 99) 

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

我试过在我的班级和导师中问过人,但我觉得他们只是在没有给我任何真正的指导的情况下让我离开,我是一名工程师而不是程序员,所以这很难绕过我的头。

+1

我没有看到'i'为'newNumbers [i]定义'请包括所有相关的源代码。 – NewToJS

+0

如果你是一名工程师,那么编程就不难了。用任何语言编程都是运用逻辑来解决问题。作为工程师,你必须遇到应用数学编程有类似的特点。所以,不要害怕多读一些关于'JavaScript'的知识,并多想一想。编程有95%的想法和5%的编码。此外,如果您在代码中使用注释更多地描述代码,那么这将对您有所帮助。 – Sand

+0

当我说“工程师”我的意思是我擅长搭建东西和数学很可能会成为俄罗斯的给我,但你的权利我只需要找到我学习像学校要我去学习的正确方法。 – user8572175

回答

0

没有单一的线或这是打破你的程序代码点。 有无数的错误,并且根本不起作用。

看看这个例子(JS ES5):

var Electorate = { 
 
    candidates: [], 
 
    init: function() { 
 
    this.candidates = []; 
 
    return this; 
 
    }, 
 
    getCandidateByName: function(name) { 
 
    var filter_candidate_by_name = this.candidates.filter(function(d) { 
 
     return d.name === name; 
 
    })[0]; 
 
    var index_of_candidate = this.candidates.indexOf(filter_candidate_by_name); 
 
    return this.candidates[index_of_candidate]; 
 
    }, 
 
    calculateWinner: function() { 
 
    var max_votes = Math.max.apply(Math, this.candidates.map(function(d) { 
 
     return d.votes.length; 
 
    })); 
 
    if (!max_votes || isNaN(max_votes)) return false; 
 
    var records_with_max_votes = this.candidates.filter(function(d) { 
 
     return d.votes.length === max_votes; 
 
    }); 
 
    var result = {}; 
 
    if (records_with_max_votes.length > 1) { 
 
     result.result = 'Tied'; 
 
     result.candidates = records_with_max_votes; 
 
     var list_of_candidate_names = records_with_max_votes.map(function(d) { 
 
     return d.name; 
 
     }).join(', '); 
 
     result.explaination = 'Tied between ' + list_of_candidate_names + ', with a count of ' + max_votes + ' votes each'; 
 
    } else if (records_with_max_votes.length === 1) { 
 
     result.result = 'Won'; 
 
     result.candidate = records_with_max_votes[0]; 
 
     result.explaination = records_with_max_votes[0].name + ' won with a count of ' + max_votes + ' votes'; 
 
    } 
 
    return result; 
 
    } 
 
}; 
 

 
var Voter = { 
 
    name: null, 
 
    age: null, 
 
    gender: null, 
 
    init: function(name, age, gender) { 
 
    if (!name || !age || !gender) return false; 
 
    this.name = name; 
 
    this.age = age; 
 
    this.gender = gender; 
 
    return this; 
 
    } 
 
}; 
 

 
var Candidate = { 
 
    name: null, 
 
    votes: [], 
 
    init: function(name) { 
 
    this.name = name; 
 
    this.votes = []; 
 
    return this; 
 
    }, 
 
    castVote: function(voter) { 
 
    this.votes.push(voter); 
 
    return this; 
 
    } 
 
}; 
 

 

 
var electorate = Object.create(Electorate).init(); 
 

 
electorate.candidates.push(
 
    Object.create(Candidate).init('Mr. John Smith'), 
 
    Object.create(Candidate).init('Mr. Adam John'), 
 
    Object.create(Candidate).init('Ms. Christina Brown') 
 
); 
 

 
electorate 
 
    .getCandidateByName('Mr. John Smith') 
 
    .castVote(Object.create(Voter).init('Maria Smith', 38, 'Female')) 
 
    .castVote(Object.create(Voter).init('John Doe', 118, 'Male')) 
 
    .castVote(Object.create(Voter).init('John Doe', 44, 'Male')); 
 

 
electorate 
 
    .getCandidateByName('Ms. Christina Brown') 
 
    .castVote(Object.create(Voter).init('John Doe', 235, 'Male')) 
 
    .castVote(Object.create(Voter).init('John Doe', 34, 'Male')) 
 
    .castVote(Object.create(Voter).init('John Doe', 22, 'Male')); 
 

 

 
console.log(electorate.calculateWinner());

你可以看到,它收集信息,并考虑到候选人和选民可以创建并添加到适当的数据位置在选民中。

然后,它可以继续所有票都投后,并宣布选定获奖者或获奖者并列。


我的建议是刷新你的Javascript知识,并尝试不使用ES6添加。

这是一个很好的资源,以JavaScript的刷(用于各种经验水平):https://github.com/getify/You-Dont-Know-JS

+0

感谢您的回复,我做了代码学院网站,并认为我有一个合理的理解,但然后我们得到了这个任务,它是完全不同的,布局和几乎所有的东西,我的头脑完全吹。所以我要通过我的直觉/谷歌/剪切/粘贴,虽然谷歌没有太大的帮助,因为我无法找到我的导师给我们的格式的东西。我只是非常困惑和吓坏了一下。 – user8572175

+0

很高兴我能帮到你。 – IzzyCooper

0

功能才能this作为参数。

有效的函数应该是这样的:

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

如果你可以分享你的整个脚本关于表决/选举程序,我可以帮助引导你和你的方法编写有效的代码。

+0

欢呼声,我可以在整个“选举”类中进行编辑,我现在正在尝试这个类,但是有几个类文件,所以我不知道如何去添加它。 – user8572175

+0

你能复制/粘贴这些文件的内容吗?而且,这整个功能不会像你可能感觉到的那样工作。 – IzzyCooper