|
| 1 | +// https://www.codewars.com/kata/587731fda577b3d1b0001196/train/javascript |
| 2 | +// eslint-disable-next-line no-extend-native |
| 3 | +String.prototype.camelCase = function() { |
| 4 | + if ( typeof this.trim() !== 'string' || !this.length ) return ''; |
| 5 | + |
| 6 | + return this |
| 7 | + .trim() |
| 8 | + .split( ' ' ) |
| 9 | + .map( ( word ) => `${word[0].toUpperCase()}${word.slice( 1 )}` ) |
| 10 | + .join( '' ); |
| 11 | +}; |
| 12 | +console.log( 'camel case method'.camelCase() ); |
| 13 | +console.log( ''.camelCase() ); |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +// https://www.codewars.com/kata/62c376ce1019024820580309/train/javascript |
| 18 | +function getCellAddresses( range ) { |
| 19 | + const [ start, end ] = range.split( ':' ); |
| 20 | + if ( start === end ) return []; |
| 21 | + |
| 22 | + const isString = new RegExp( /\D/gi ); |
| 23 | + const isNumber = new RegExp( /\d/gi ); |
| 24 | + const startLetter = String( start.match( isString ).join( '' ) ); |
| 25 | + const startNumber = Number( start.match( isNumber ).join( '' ) ); |
| 26 | + const endLetter = String( end.match( isString ).join( '' ) ); |
| 27 | + const endNumber = Number( end.match( isNumber ).join( '' ) ); |
| 28 | + |
| 29 | + const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' ); |
| 30 | + const sheet = []; |
| 31 | + |
| 32 | + for ( let i = startNumber; i <= endNumber; i++ ) { |
| 33 | + for ( let j = alpha.indexOf( startLetter ); j <= alpha.indexOf( endLetter ); j++ ) { |
| 34 | + sheet.push( alpha[j] + i ); |
| 35 | + } |
| 36 | + } |
| 37 | + return sheet; |
| 38 | +} |
| 39 | +// console.log( getCellAddresses( 'B3:D5' ) ); |
| 40 | +console.log( getCellAddresses( 'B86:L212' ) ); |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +// https://www.codewars.com/kata/52efefcbcdf57161d4000091/train/javascript |
| 45 | +function count( string ) { |
| 46 | + return string |
| 47 | + .split( '' ) |
| 48 | + .reduce( ( acc, val ) => { |
| 49 | + acc[val] ? acc[val]++ : acc[val] = 1; |
| 50 | + return acc; |
| 51 | + }, {} ); |
| 52 | +} |
| 53 | +console.log( count( 'AAABCC' ) ); |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +// https://www.codewars.com/kata/5274e122fc75c0943d000148/train/javascript |
| 58 | +function groupByCommas( n ) { |
| 59 | + return n.toString() |
| 60 | + .split( '' ) |
| 61 | + .reverse().join( '' ) |
| 62 | + .replace( /\d{3}/g, '$&,' ) |
| 63 | + .split( '' ) |
| 64 | + .reverse( '' ) |
| 65 | + .join( '' ) |
| 66 | + .replace( /^,/, '' ); |
| 67 | +} |
| 68 | +console.log( groupByCommas( 1 ) ); |
| 69 | +console.log( groupByCommas( 10 ) ); |
| 70 | +console.log( groupByCommas( 100 ) ); |
| 71 | +console.log( groupByCommas( 1000 ) ); |
| 72 | +console.log( groupByCommas( 10000 ) ); |
| 73 | +console.log( groupByCommas( 100000 ) ); |
| 74 | +console.log( groupByCommas( 1000000 ) ); |
| 75 | +console.log( groupByCommas( 10000000 ) ); |
| 76 | +console.log( groupByCommas( 100000000 ) ); |
| 77 | +console.log( groupByCommas( 1000000000 ) ); |
| 78 | + |
| 79 | +// others solutions... *ouch* |
| 80 | +function groupByCommas2( n ) { |
| 81 | + return n.toLocaleString(); |
| 82 | +} |
| 83 | +console.log( groupByCommas2( 1 ) ); |
| 84 | +console.log( groupByCommas2( 10 ) ); |
| 85 | +console.log( groupByCommas2( 100 ) ); |
| 86 | +console.log( groupByCommas2( 1000 ) ); |
| 87 | +console.log( groupByCommas2( 10000 ) ); |
| 88 | +console.log( groupByCommas2( 100000 ) ); |
| 89 | +console.log( groupByCommas2( 1000000 ) ); |
| 90 | +console.log( groupByCommas2( 10000000 ) ); |
| 91 | +console.log( groupByCommas2( 100000000 ) ); |
| 92 | +console.log( groupByCommas2( 1000000000 ) ); |
| 93 | + |
| 94 | +function groupByCommas3( n ) { |
| 95 | + return n.toString().replace( /\B(?=(\d{3})+(?!\d))/g, ',' ); |
| 96 | +} |
| 97 | +console.log( groupByCommas3( 1 ) ); |
| 98 | +console.log( groupByCommas3( 10 ) ); |
| 99 | +console.log( groupByCommas3( 100 ) ); |
| 100 | +console.log( groupByCommas3( 1000 ) ); |
| 101 | +console.log( groupByCommas3( 10000 ) ); |
| 102 | +console.log( groupByCommas3( 100000 ) ); |
| 103 | +console.log( groupByCommas3( 1000000 ) ); |
| 104 | +console.log( groupByCommas3( 10000000 ) ); |
| 105 | +console.log( groupByCommas3( 100000000 ) ); |
| 106 | +console.log( groupByCommas3( 1000000000 ) ); |
0 commit comments