Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

backbone.js - create a custom method get experience of the employee model by considering the year of joining as parameter

Create a custom method get experience of the employee model by considering the year of joining as parameter

Create Employee model with properties employee_id, name, year_of_joining and experience

question from:https://stackoverflow.com/questions/65651866/create-a-custom-method-get-experience-of-the-employee-model-by-considering-the-y

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the question asked by Hacker Rank there are few test cases out of that one generally fails .

which says expected 22 instead of 0 .

So the complete answer is posted here.

So the Answer to the question is

var Employee = Backbone.Model.extend({ //Write your code here

default:
{
   employee_id:1111,
   name:'Sarah Roe',
   year_of_joining:1999,
   address:'ABC Street',
   experience: null
},
getExperience: function (year_of_joining) {
  var current_date = new Date();
  var current_year = current_date.getFullYear();
  var calculated_exp = (current_year) - (year_of_joining);

  *this.set({ experience: calculated_exp });*
  return calculated_exp;
}

});

var employee = new Employee({ employee_id: 721, name: "Shrikrishna", year_of_joining: 1999, experience: 22}); //please add properties and values of Employee model //employee.set({'experience': employee.get('year_of_joining')}) ; alert("Experience of " + employee.get("name") + " is " + employee.getExperience(employee.get('year_of_joining')) + " years");

</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...