Skip to content

Instantly share code, notes, and snippets.

@cmoore4
Created October 15, 2011 05:52
Show Gist options
  • Save cmoore4/1289114 to your computer and use it in GitHub Desktop.
Save cmoore4/1289114 to your computer and use it in GitHub Desktop.
Javascript Brief Resume
/*
* In Response to: http://tampa.craigslist.org/hil/cpg/2630542650.html
*
*/
var Applicant = function(args){
return {
'name': args.name || 'Jos Shmo',
'skills': args.skills || {'Not': 0, 'The': 1, 'Right': 0, 'Skills': 1},
'address': args.address || {'street': '123 Spaghetti Code Drive', 'city': 'BadCodersVille', 'state': 'CA'},
'attributes': {
"Implements clean code": true,
"Codes with reusability in mind": true,
"Knows modern JS tools and techniques": true,
"Works well as both a leader and a member of a team": true
},
'best candidate': args['best candidate'] || false,
'toString': function(){
var address = '';
var skills = [];
var skillTotal = 0;
for (k in this.address){
address += this.address[k] + '\n';
}
for (k in this.skills){
skills.push(k);
skillTotal += this.skills[k];
}
return this.name + '\n' + address + '\nSkillset: ' + skills.join(', ') + ' (' + skillTotal + ')';
},
'print': function(){console.log(this.toString());}
};
};
var applicants = {};
applicants.sean = new Applicant({
'name': 'Sean Moore',
'skills': {'Node.js': 84, 'Javascript': 92, 'HTML5': 72, 'Python': 74, 'Linux': 73, 'PHP': 94, 'CSS3': 52, 'Mobile Web': 58, 'iOS': 1, 'OS X': 1},
'address': {
'street': '1533 Dublin St.',
'city': 'New Orleans',
'state': 'LA',
'zip': 70118,
'cell': '504-214-4595'
},
'best candidate': true
});
applicants.others = new Applicant({});
/*
for (k in applicants){
if (applicants[k].hasOwnProperty('best candidate')){
if (applicants[k]['best candidate'] === true){
applicants[k].print();
}
}
}*/
// CSM: Implemented stub function for code completion sake.
function hire(applicant){
console.log('HIRED!:\n' + applicant);
}
function applyForJob( applicant ) {
var skillPoints = 0;
// Skills an applicant needs.
var requiredSkills = [ "Javascript", "CSS3", "HTML5", "Mobile Web", "OS X" ];
// These skills would be super.
var extraSkills = [ "Python", "Node.js", "iOS", "Linux" ];
// Attributes needed in an applicant.
var attributes = [
"Implements clean code",
"Codes with reusability in mind",
"Knows modern JS tools and techniques",
"Works well as both a leader and a member of a team"
];
// Check attributes.
// CSM: For...in is a bad choice to use on the array here, for the same reasons as mentioned below.
// I've modified it to work. But comparing an array to an object is not wise, nor pretty to look at.
// First, we make sure the object has the property name of the array value, then we check to see if
// the object value is true. This prevents unknown property errors.
for ( attributeName in attributes ) {
if ( !(applicant['attributes'].hasOwnProperty(attributes[attributeName]) && applicant['attributes'][attributes[attributeName]]) ) return console.log('Applicant Missing attribute: ' + attributes[attributeName]);
}
// Add up the skills.
// CSM: This was funky. Should not use a for...in on arrays, especially unnamed arrays, it makes them
// hard to reference, you are not guaranteed order, and it picks up other properties of any modified
// array prototype. This version works, though: (The same method could be applied above)
/*
for ( skillNum in requiredSkills.concat( extraSkills ) ) {
var skillName = requiredSkills.concat(extraSkills)[skillNum];
skillPoints += applicant.skills[skillName];
console.log(applicant.name + ' current points: ' + skillPoints);
}
*/
// Better version:
for (var skillNum =0, carr = requiredSkills.concat(extraSkills), skillLen = carr.length; skillNum<skillLen; skillNum++){
var skillName = carr[skillNum];
skillPoints += applicant.skills[skillName] || 0;
console.log(applicant.name + ' current points: ' + skillPoints);
}
// Do we hire this applicant?
if ( skillPoints > 500 ) hire(applicant);
}
applyForJob(applicants.sean);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment