Last active
January 21, 2016 06:02
-
-
Save kavitshah8/2e836c122ac76dcabdd7 to your computer and use it in GitHub Desktop.
Company Specific
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function compressArr (arr) { | |
var start = arr[0]; | |
var stop = start; | |
var arrLength = arr.length; | |
var result = ''; | |
for (var i = 1; i < arrLength; i++) { | |
if (arr[i] === stop+1) { | |
stop = arr[i]; | |
} else { | |
if (start === stop) { | |
result += start + ', '; | |
} else { | |
result += start + '-' + stop + ', '; | |
} | |
// reset the start and stop pointers | |
start = arr[i]; | |
stop = start; | |
} | |
} | |
if (start === stop) { | |
result += start; | |
} else { | |
result += start + '-' + stop; | |
} | |
console.log(result); | |
} | |
var arr = [1, 2, 3, 10, 25, 26, 30, 31, 32, 33]; | |
compressArr(arr); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function cropSentence(sen) { | |
console.log('Sentence length = ', sen.length); | |
console.log('140 th character = ', sen.charAt(139)); | |
if (sen.length > 140) { | |
if (sen[140] === ' ') { | |
sen = sen.substring(0, 140); | |
} else { | |
for (var i = 139; i >= 0; i--) { | |
if (sen[i] === ' ') { | |
sen = sen.substring(0, i + 1); | |
break; | |
} | |
} | |
} | |
} | |
return sen; | |
} | |
console.log(cropSentence('Foooo ')); | |
console.log('\n'); | |
console.log(cropSentence('Foooo kdnfs asfdnks sdfn asfnas fasdfk asflk saf sfm asfd mlmaf asf saf asfsafd dfdsf fdsf dsf sdf fffffffffffffffffffffffffff ffffff hhhXY f ')); | |
console.log('\n'); | |
console.log(cropSentence('Foooo kdnfs asfdnks sdfn asfnas fasdfk asflk saf sfm asfd mlmaf asf saf asfsafd dfdsf fdsf dsf sdf fffffffffffffffffffffffffff ffffff hhh XYf ')); | |
console.log('\n'); | |
console.log(cropSentence('Foooo kdnfs asfdnks sdfn asfnas fasdfk asflk saf sfm asfd mlmaf asf saf asfsafd dfdsf fdsf dsf sdf fffffffffffffffffffffffffff ffffff hhhXYf ')); | |
console.log('\n'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var withdraw = require('withdraw'); // This function is provided | |
function withdrawAllMoney () { | |
var allMoney = 0; | |
var value = Number.MAX_VALUE; | |
while (value) { | |
if (!withdraw(value)) { | |
value /= 2; | |
} else { | |
allMoney += value; | |
} | |
} | |
return allMoney; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment