• Find The Parity Outlier

[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11 (the only odd number)

[160, 3, 1719, 19, 11, 13, -21]
Should return: 160 (the only even number)

  1. // mine
  2. function findOutlier(int){
  3. const arr = int.map(v => v%2===0)
  4. let index = 0
  5. arr.map(v => {
  6. if(arr.indexOf(v) === arr.lastIndexOf(v)){
  7. index = arr.indexOf(v)
  8. }
  9. })
  10. return int[index]
  11. }
  1. // the best
  2. function findOutlier(int){
  3. var even = int.filter(a=>a%2==0);
  4. var odd = int.filter(a=>a%2!==0);
  5. return even.length==1? even[0] : odd[0];
  6. }
  • Format a string of names like ‘Bart, Lisa & Maggie’

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 ‘’

  1. // mine
  2. function list(names){
  3. const arr = names.map(v => v.name)
  4. let str = arr.join(', ')
  5. if(arr.length === 0 || arr.length === 1) return str
  6. let index = str.lastIndexOf(',')
  7. const newArr = str.split('')
  8. newArr.splice(index, 1, ' &')
  9. return newArr.join('')
  10. }
  1. // the best
  2. function list(names){
  3. return names.reduce(function(prev, current, index, array){
  4. if (index === 0){
  5. return current.name;
  6. }
  7. else if (index === array.length - 1){
  8. return prev + ' & ' + current.name;
  9. }
  10. else {
  11. return prev + ', ' + current.name;
  12. }
  13. }, '');
  14. }
  • Jaden Casing Strings

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”

  1. // mine
  2. String.prototype.toJadenCase = function () {
  3. return this.replace(/(^|\s)\S/g, v => v.toUpperCase())
  4. };
  1. // the best
  2. String.prototype.toJadenCase = function () {
  3. return this.split(" ").map(function(word){
  4. return word.charAt(0).toUpperCase() + word.slice(1);
  5. }).join(" ");
  6. }

2019/6/25

  • Sum of the first nth term of Series

SeriesSum(1) => 1 = “1.00”
SeriesSum(2) => 1 + 1/4 = “1.25”
SeriesSum(5) => 1 + 1/4 + 1/7 + 1/10 + 1/13 = “1.57”

  1. // mine
  2. function SeriesSum(n) {
  3. return [...Array(n).keys()].reduce((acc, cur) => acc + 1 / (1 + cur * 3), 0).toFixed(2)
  4. }
  1. // the best
  2. function SeriesSum(n, s = 0) {
  3. return n ? SeriesSum(n - 1, s + 1 / (3 * n - 2)) : s.toFixed(2)
  4. }
  • Exes and Ohs(统计相同数量的o和x)

  1. // mine
  2. function XO(str) {
  3. const newStr = str.toLowerCase();
  4. const obj = {};
  5. for(let i=0;i<newStr.length;i++){
  6. obj[newStr[i]] ? obj[newStr[i]]+=1 : obj[newStr[i]]=0
  7. }
  8. return (obj.x===obj.o)
  9. }
  1. // the best
  2. function XO(str) {
  3. let x = str.match(/x/gi);
  4. let o = str.match(/o/gi);
  5. return (x && x.length) === (o && o.length);
  6. }
  • Equal Sides Of An Array

找到一个索引N,其中N左边的整数和等于右边的整数和,如果没有请返回 -1

  1. Test.assertEquals(findEvenIndex([1,2,3,4,3,2,1]),3, "The array was: [1,2,3,4,3,2,1] \n");
  2. Test.assertEquals(findEvenIndex([1,100,50,-51,1,1]),1, "The array was: [1,100,50,-51,1,1] \n");
  3. Test.assertEquals(findEvenIndex([1,2,3,4,5,6]),-1, "The array was: [1,2,3,4,5,6] \n");
  4. Test.assertEquals(findEvenIndex([20,10,30,10,10,15,35]),3, "The array was: [20,10,30,10,10,15,35] \n");
  1. // mine
  2. function findEvenIndex(arr)
  3. {
  4. const bool = arr.map((v,i) => {
  5. const left = arr.slice(0,i)
  6. const right = arr.slice(i+1,arr.length)
  7. return (left.length === 0 ? 0 : left.reduce((a,b)=>a+b)) === (right.length === 0 ? 0 : right.reduce((a,b)=>a+b))
  8. })
  9. return bool.indexOf(true)
  10. }
  1. // the best
  2. function findEvenIndex(arr)
  3. {
  4. for(var i=1; i<arr.length-1; i++) {
  5. if(arr.slice(0, i).reduce((a, b) => a+b) === arr.slice(i+1).reduce((a, b) => a+b)) {
  6. return i;
  7. }
  8. }
  9. return -1;
  10. }
  • Does my number look big in this?

自恋数是一个数字,它是自己数字的总和,每个数字都增加到给定基数中的数字位数。
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

  1. // mine
  2. function narcissistic(value) {
  3. return value.toString().split('').reduce((acc, cur, index, arr) => acc + Math.pow(cur, arr.length), 0) === value
  4. }
  1. // the best
  2. function narcissistic( value ) {
  3. return ('' + value).split('').reduce(function(p, c){
  4. return p + Math.pow(c, ('' + value).length)
  5. }, 0) == value;
  6. }

2019/6/26

  • IQ Test

iqTest(“2 4 7 8 10”) => 3 // Third number is odd, while the rest of the numbers are even
iqTest(“1 2 1 1”) => 2 // Second number is even, while the rest of the numbers are odd

  1. // mine
  2. function iqTest(numbers){
  3. const arr = numbers.split(' ').map(v => v%2 === 0)
  4. for(let i=0;i<arr.length;i++){
  5. if(arr.indexOf(arr[i]) === arr.lastIndexOf(arr[i])){
  6. return i+1
  7. }
  8. }
  9. }
  1. // the best
  2. function iqTest(numbers){
  3. numbers = numbers.split(" ").map(function(el){return parseInt(el)});
  4. var odd = numbers.filter(function(el){ return el % 2 === 1});
  5. var even = numbers.filter(function(el){ return el % 2 === 0});
  6. return odd.length < even.length ? (numbers.indexOf(odd[0]) + 1) : (numbers.indexOf(even[0]) + 1);
  7. }

2019/6/27

  • Find the missing letter

[‘a’,’b’,’c’,’d’,’f’] -> ‘e’
[‘O’,’Q’,’R’,’S’] -> ‘P’
(Use the English alphabet with 26 letters!)

  1. // mine
  2. function findMissingLetter(array)
  3. {
  4. const arr = 'abcdefghijklmnopqrstuvwxyz'
  5. const ARR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  6. for (let i=0;i<array.length;i++){
  7. if(arr.indexOf(array[i]) >= 0) {
  8. if(arr.indexOf(array[i]) !== arr.indexOf(array[i+1])-1){
  9. return arr[arr.indexOf(array[i])+1]
  10. }
  11. } else {
  12. if(ARR.indexOf(array[i]) !== ARR.indexOf(array[i+1])-1){
  13. return ARR[ARR.indexOf(array[i])+1]
  14. }
  15. }
  16. }
  17. }
  1. // the best
  2. function findMissingLetter(array)
  3. {
  4. var i=array[0].charCodeAt();
  5. array.map(x=> x.charCodeAt()==i?i++:i);
  6. return String.fromCharCode(i);
  7. }
  • Roman Numerals Encoder

solution(1000); // should return ‘M’
把数字转化为罗马数字

  1. // help
  2. Symbol Value
  3. I 1
  4. V 5
  5. X 10
  6. L 50
  7. C 100
  8. D 500
  9. M 1,000
  1. // the best
  2. function solution(number){
  3. // convert the number to a roman numeral
  4. var roman = {M:1000,CM:900, D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1 }
  5. var ans = '';
  6. while(number>0){
  7. for(a in roman){
  8. if(roman[a]<=number){ ans += a; number-=roman[a]; break;}
  9. }
  10. }
  11. return ans;
  12. }
  • Stop gninnipS My sdroW!

spinWords( “Hey fellow warriors” ) => returns “Hey wollef sroirraw”
spinWords( “This is a test”) => returns “This is a test”
spinWords( “This is another test” )=> returns “This is rehtona test”
翻转长度大于等于5的单词

  1. // mine
  2. function spinWords(str){
  3. return str.split(' ').map(v => v.length>=5 ? v.split('').reverse().join('') : v ).join(' ')
  4. }
  1. // the best
  2. function spinWords(string){
  3. return string.replace(/\w{5,}/g, function(w) { return w.split('').reverse().join('') })
  4. }

2019/6/28

  • Persistent Bugger

``` persistence(39) === 3 // because 39 = 27, 27 = 14, 1*4=4 // and 4 has only one digit

persistence(999) === 4 // because 999 = 729, 729 = 126, // 126 = 12, and finally 1*2 = 2

persistence(4) === 0 // because 4 is already a one-digit number

// mine function persistence(num) { let index = 0 let result = num while (result.toString().length !== 1) { console.log(result) result = result.toString().split(‘’).reduce((acc, cur) => acc*cur, 1) index += 1 } return index }

// the best const persistence = num => { return ${num}.length > 1 ? 1 + persistence(${num}.split(‘’).reduce((a, b) => a * +b)) : 0; }

  1. -
  2. <a name="58bc7a3f"></a>
  3. # Is a number prime
  4. > is_prime(1) /_ false _/<br />
  5. is_prime(2) /_ true _/<br />
  6. is_prime(-1) /_ false _/<br />
  7. 是否为质数

// mine function isPrime(num) { let result = 0 for (let i=1;i<=num;i++){ num % i === 0 ? result += 1 : null } return result === 2 } // function isPrime(num) { // return […Array(num).keys()].reduce((acc, cur, index) => num%(index+1)===0?acc+=1:acc,0) === 2 // } // 此方法会出现数组最大长度BUG

// the best function isPrime(num) { for (var i = 2; i < num; i++) if (num % i == 0) return false; return num >= 2; }

  1. -
  2. <a name="bbb82190"></a>
  3. # Where my anagrams at?
  4. >

‘abba’ & ‘baab’ == true

‘abba’ & ‘bbaa’ == true

‘abba’ & ‘abbba’ == false

‘abba’ & ‘abca’ == false

anagrams(‘abba’, [‘aabb’, ‘abcd’, ‘bbaa’, ‘dada’]) => [‘aabb’, ‘bbaa’]

anagrams(‘racer’, [‘crazer’, ‘carer’, ‘racar’, ‘caers’, ‘racer’]) => [‘carer’, ‘racer’]

anagrams(‘laser’, [‘lazing’, ‘lazy’, ‘lacer’]) => []

// mine function anagrams(word, words) { return words.filter(v => v.split(‘’).sort().join(‘’) === word.split(‘’).sort().join(‘’)) }

// the best String.prototype.sort = function() { return this.split(“”).sort().join(“”); };

function anagrams(word, words) { return words.filter(function(x) { return x.sort() === word.sort(); }); }

  1. <a name="394dd5e6"></a>
  2. # _2019/7/9_
  3. -
  4. <a name="Permutations"></a>
  5. # Permutations
  6. > 您必须创建输入字符串的所有排列并删除重复项(如果存在)。
  7. >

permutations(‘a’); // [‘a’] permutations(‘ab’); // [‘ab’, ‘ba’] permutations(‘aabb’); // [‘aabb’, ‘abab’, ‘abba’, ‘baab’, ‘baba’, ‘bbaa’]

// mine
function permutations(string) { let array = string.split(‘’) let arr = [] for (let i = 0; i < 10000; i++) { let str = array.sort(() => { return Math.random() - 0.5 }).join(‘’) arr.push(str) } return Array.from(new Set(arr)) }

// the best function permutations(str) { return (str.length <= 1) ? [str] : Array.from(new Set( str.split(‘’) .map((char, i) => permutations(str.substr(0, i) + str.substr(i + 1)).map(p => char + p)) .reduce((r, x) => r.concat(x), []) )); }

  1. <a name="87202701"></a>
  2. # _2019/7/14_
  3. -
  4. <a name="6983d9fe"></a>
  5. # First non-repeating character
  6. > 'a' => 'a'<br />
  7. 'stress' => 't'<br />
  8. 'Moonmen' => 'e'

// mine function firstNonRepeatingLetter(s) { const result = [] s.toLowerCase().split(‘’).filter((v,i,arr) => { if( arr.indexOf(v) === arr.lastIndexOf(v) ) { console.log(v, i, s.split(‘’)[i]) result.push(s.split(‘’)[i]) } }) return result[0] || ‘’ }

// the best function firstNonRepeatingLetter(s) { var t=s.toLowerCase(); for (var x=0;x<t.length;x++) if(t.indexOf(t[x]) === t.lastIndexOf(t[x])) return s[x]; return “”; } ```