diff --git a/src/array.js b/src/array.js index f458478..b877be8 100644 --- a/src/array.js +++ b/src/array.js @@ -11,7 +11,12 @@ * [0, 1, 2, 3, 4, 5], 5 => 5 */ function findElement(arr, value) { - throw new Error("Not implemented"); + for(let i=0; i < arr.length; i++){ + if(arr[i]==value){ + return i; + } + } + return -1; } /** @@ -27,7 +32,11 @@ function findElement(arr, value) { * [] => [] */ function doubleArray(arr) { - throw new Error("Not implemented"); + let k=arr.length; + for( let i=0; i < k ; i++){ + arr[i+k]=arr[i]; + } + return arr; } /** @@ -42,7 +51,7 @@ function doubleArray(arr) { * [] => [] */ function getArrayOfPositives(arr) { - throw new Error("Not implemented"); + return arr.filter(n => n>0); } /** @@ -59,7 +68,7 @@ function getArrayOfPositives(arr) { * [ false, 0, NaN, '', undefined ] => [ ] */ function removeFalsyValues(arr) { - throw new Error("Not implemented"); + return arr.filter(Boolean); } /** @@ -73,7 +82,12 @@ function removeFalsyValues(arr) { * [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ] */ function getStringsLength(arr) { - throw new Error("Not implemented"); + let k=arr.length; + let str=[]; + for(let i=0;i 1111 */ function getItemsSum(arr) { - throw new Error("Not implemented"); -} + let sum=0; + for(let i=0;i 3628800 */ function getFactorial(n) { - throw new Error("Not implemented"); + let result=1; + for(let i=1 ; i<=n;i++){ + result=result*i; + } + return result; } /** @@ -50,7 +59,12 @@ function getFactorial(n) { * -1,1 => 0 ( = -1 + 0 + 1 ) */ function getSumBetweenNumbers(n1, n2) { - throw new Error("Not implemented"); + let sum=0; + for(let i=n1; i<=n2; i++){ + sum=sum + i; + } + return sum; + } /** @@ -69,7 +83,10 @@ function getSumBetweenNumbers(n1, n2) { * 10,10,10 => true */ function isTriangle(a, b, c) { - throw new Error("Not implemented"); + if( (a+b)>c && (b+c)>a && (a+c)>b ){ + return true; + } + else{return false;} } /** @@ -85,7 +102,7 @@ function isTriangle(a, b, c) { * 'noon' => 'noon' */ function reverseString(str) { - throw new Error("Not implemented"); + return str.split("").reverse().join(''); } /** @@ -109,10 +126,21 @@ function reverseString(str) { * '{)' = false * '{[(<{[]}>)]}' = true */ -function isBracketsBalanced(str) { - throw new Error("Not implemented"); -} +function isBracketsBalanced(str) { + const stack = []; + const brackets = {'(': ')', '[': ']', '{': '}', '<': '>'}; + + for (const char of str) { + if (brackets.hasOwnProperty(char)) { + stack.push(char); + } else if (brackets[stack.pop()] !== char) { + return false; + } + } + + return stack.length === 0; + } /** * Returns the human readable string of time period specified by the start and end time. * The result string should be constrcuted using the folliwing rules: @@ -145,9 +173,38 @@ function isBracketsBalanced(str) { * */ function timespanToHumanString(startDate, endDate) { - throw new Error("Not implemented"); -} - +const timeDifference = (endDate - startDate) ; + const seconds = Math.round(timeDifference / 1000); + const minutes = Math.round(timeDifference / (1000*60)); + const hours = Math.round(timeDifference / (1000*60*60)); + const days = Math.round(timeDifference / (1000*60*60*24)); + const months = Math.round(timeDifference / (1000*60*60*24*30)); + const years = Math.round(timeDifference / (1000*60*60*24*30*12)); + + if ( seconds < 45) { + return "a few seconds ago"; + } else if (seconds <= 90) { + return "a minute ago"; + } else if (minutes <= 45) { + return `${minutes} minutes ago`; + } else if (minutes <= 90) { + return "an hour ago"; + } else if (hours <= 22) { + return `${hours} hours ago`; + } else if (hours <= 36) { + return "a day ago"; + } else if (days <= 25) { + return `${days} days ago`; + } else if (days <= 45) { + return "a month ago"; + } else if (days <= 345) { + return `${months} months ago`; + } else if (days <= 545) { + return "a year ago"; + } else { + return `${years} years ago`; + } + } /** * Returns the string with n-ary (binary, ternary, etc, where n<=10) representation of * specified number. @@ -169,7 +226,17 @@ function timespanToHumanString(startDate, endDate) { * 365, 10 => '365' */ function toNaryString(num, n) { - throw new Error("Not implemented"); + + if(num==0){ + return "0"; + } + var toNaryString = ""; + while(num>0){ + var remainder=num%n; + toNaryString = remainder+toNaryString; + num=Math.floor(num/n); + } + return toNaryString; } module.exports = { diff --git a/src/numbers.js b/src/numbers.js index 276fca0..b0513da 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -10,7 +10,7 @@ * 5, 5 => 25 */ function getRectangleArea(width, height) { - throw new Error("Not implemented"); + return (width*height); } /** @@ -26,7 +26,10 @@ function getRectangleArea(width, height) { * -3, 3 => 0 */ function getAverage(value1, value2) { - throw new Error("Not implemented"); + var k1 = value1/2; + var k2= value2/2; + let avg= k1+k2; + return avg; } /** @@ -42,7 +45,8 @@ function getAverage(value1, value2) { * 5*x = 0 => 0 */ function getLinearEquationRoot(a, b) { - throw new Error("Not implemented"); + if(b==0){return 0;} + else{return -(b/a);} } /** @@ -58,7 +62,8 @@ function getLinearEquationRoot(a, b) { * 0 => 0 */ function getLastDigit(value) { - throw new Error("Not implemented"); + if(value<0){return (-value)%10;} + else{return value%10;} } /** @@ -73,7 +78,8 @@ function getLastDigit(value) { * '-525.5' => -525.5 */ function parseNumberFromString(value) { - throw new Error("Not implemented"); + let num=parseFloat(value); + return num; } /** @@ -94,7 +100,14 @@ function parseNumberFromString(value) { * 17 => true */ function isPrime(n) { - throw new Error("Not implemented"); + if( n==2 || n==3) + return true; + if(n<=1|| n%2 ==0 || n%3 ==0) + return false; +for(let i=5;i*i<=n;i+=6) +if(n%i==0 || n%(i+2)==0) + return false; +return true; } module.exports = { diff --git a/src/strings.js b/src/strings.js index 28f682a..0d119c1 100644 --- a/src/strings.js +++ b/src/strings.js @@ -11,7 +11,7 @@ * '', 'bb' => 'bb' */ function concatenateStrings(value1, value2) { - throw new Error("Not implemented"); + return value1+value2 ; } /** @@ -26,7 +26,7 @@ function concatenateStrings(value1, value2) { * '' => 0 */ function getStringLength(value) { - throw new Error("Not implemented"); + return value.length; } /** @@ -40,7 +40,8 @@ function getStringLength(value) { * 'cat' => 'c' */ function getFirstChar(value) { - throw new Error("Not implemented"); + let arr = value.split(''); + return arr[0]; } /** @@ -55,7 +56,8 @@ function getFirstChar(value) { * '\tHello, World! ' => 'Hello, World!' */ function removeLeadingAndTrailingWhitespaces(value) { - throw new Error("Not implemented"); + let str=value.trim(); + return str; } /** @@ -70,7 +72,11 @@ function removeLeadingAndTrailingWhitespaces(value) { * 'cat', 3 => 'catcatcat' */ function repeatString(value, count) { - throw new Error("Not implemented"); + let result=''; + for(let i=0; i 'ABAB' */ function removeFirstOccurrences(str, value) { - throw new Error("Not implemented"); + let index = str.indexOf(value); + if(index==-1){return str;} + return str.slice(0,index)+str.slice(index+ value.length); } /** @@ -100,7 +108,7 @@ function removeFirstOccurrences(str, value) { * 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' */ function convertToUpperCase(str) { - throw new Error("Not implemented"); + return str.toUpperCase(); } /** @@ -120,7 +128,10 @@ function convertToUpperCase(str) { * */ function encodeToRot13(str) { - throw new Error("Not implemented"); + return str.replace(/[a-zA-Z]/g,function(c){ + let base=c <= 'Z' ? 65:97; + return String.fromCharCode((c.charCodeAt(0)-base+13)%26+base); + }); } module.exports = {