一. Square Every Digit
Note: The function accepts an integer and returns an integer
function squareDigits(num){
return Number((''+num).split('').map(function(val){return val*val;}).join(''));
study: join() & split() & map()
二.Convert strings to how they would be written by Jaden Smith.
Example:
Not Jaden-Cased: “How can mirrors be real if our eyes aren’t real”
Jaden-Cased: “How Can Mirrors Be Real If Our Eyes Aren’t Real”
String.prototype.toJadenCase = function(){
return this.split(" ").map(function(word){
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(" ");
}
三.Create a function that takes an integer as an argument and returns “Even” for even numbers or “Odd” for odd numbers.
function even_or_odd(number) {
return number % 2 ? "Odd" : "Even"
}
study:Boolean(odd):true; Boolean(even):false
四.Who likes it?
Note: Given an array, find the int that appears an odd number of times.There will always be only one integer that appears an odd number of times.
function likes(names) {
names = names || [];
swith(names.length){
case 0:return 'no one likes this';break;
case 1:return names[0] + 'likes this';break;
case 2:return names[0] + 'and" + names[1] + 'like this';break;
case 3;return names[0] + ',' + names[1] + 'and' + names[2] + 'like this';break;
default: return names[0] + ', ' + names[1] + ' and ' + (names.length - 2) + ' others like this';
}
}
study: names = names || []; swith default
五.Find the smallest integer in the array
class SmallestIntegerFinder {
findSmallestInt(args) {
return Math.min(args);
}
}
六.Format a string of names like ‘Bart, Lisa & Maggie’.
Given: an array containing hashes of names
Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.
Example:
list([ {name: ‘Bart’}, {name: ‘Lisa’}, {name: ‘Maggie’} ])
// returns ‘Bart, Lisa & Maggie’
list([ {name: ‘Bart’}, {name: ‘Lisa’} ])
// returns ‘Bart & Lisa’
list([ {name: ‘Bart’} ])
// returns ‘Bart’
list([])
// returns ”
function list(names){
return names.reduce(function(prep, current, index, array){
if(index === 0){
return current.name;
}
else if(index === array.length-1){
return previous + '&' + current.name;
}
else {
return prev + ',' + current.name;
}
}, '');
}
function list(name) {
var xs = names.map(p=>p.name)
var x = xs.pop()
return xs.length ? xs.join(",") + "&" + x: x||""
}