Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js_assignment #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -42,7 +51,7 @@ function doubleArray(arr) {
* [] => []
*/
function getArrayOfPositives(arr) {
throw new Error("Not implemented");
return arr.filter(n => n>0);
}

/**
Expand All @@ -59,7 +68,7 @@ function getArrayOfPositives(arr) {
* [ false, 0, NaN, '', undefined ] => [ ]
*/
function removeFalsyValues(arr) {
throw new Error("Not implemented");
return arr.filter(Boolean);
}

/**
Expand All @@ -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<arr.length;i++){
str[i]=arr[i].length;
}
return str;
}

/**
Expand All @@ -89,8 +103,12 @@ function getStringsLength(arr) {
* [ 1, 10, 100, 1000 ] => 1111
*/
function getItemsSum(arr) {
throw new Error("Not implemented");
}
let sum=0;
for(let i=0;i<arr.length; i++){
sum=sum+arr[i];
}
return sum;
}

module.exports = {
findElement,
Expand Down
91 changes: 79 additions & 12 deletions src/conditionalAndLoops.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
*
*/
function getFizzBuzz(num) {
throw new Error("Not implemented");
if((num%3)!=0 && (num%5)!=0){return num;}
else{
if((num%3)==0 && (num%5)!=0){return 'Fizz';}
else{if((num%3)!=0 && (num%5)==0){return 'Buzz';}
else{if((num%3)==0 && (num%5)==0){return 'FizzBuzz';}}}
}
}

/**
Expand All @@ -34,7 +39,11 @@ function getFizzBuzz(num) {
* 10 => 3628800
*/
function getFactorial(n) {
throw new Error("Not implemented");
let result=1;
for(let i=1 ; i<=n;i++){
result=result*i;
}
return result;
}

/**
Expand All @@ -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;

}

/**
Expand All @@ -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;}
}

/**
Expand All @@ -85,7 +102,7 @@ function isTriangle(a, b, c) {
* 'noon' => 'noon'
*/
function reverseString(str) {
throw new Error("Not implemented");
return str.split("").reverse().join('');
}

/**
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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 = {
Expand Down
25 changes: 19 additions & 6 deletions src/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 5, 5 => 25
*/
function getRectangleArea(width, height) {
throw new Error("Not implemented");
return (width*height);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);}
}

/**
Expand All @@ -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;}
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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 = {
Expand Down
27 changes: 19 additions & 8 deletions src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* '', 'bb' => 'bb'
*/
function concatenateStrings(value1, value2) {
throw new Error("Not implemented");
return value1+value2 ;
}

/**
Expand All @@ -26,7 +26,7 @@ function concatenateStrings(value1, value2) {
* '' => 0
*/
function getStringLength(value) {
throw new Error("Not implemented");
return value.length;
}

/**
Expand All @@ -40,7 +40,8 @@ function getStringLength(value) {
* 'cat' => 'c'
*/
function getFirstChar(value) {
throw new Error("Not implemented");
let arr = value.split('');
return arr[0];
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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<count ; i++){
result=result+value;
}
return result;
}

/**
Expand All @@ -86,7 +92,9 @@ function repeatString(value, count) {
* 'ABABAB','BA' => '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);
}

/**
Expand All @@ -100,7 +108,7 @@ function removeFirstOccurrences(str, value) {
* 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
*/
function convertToUpperCase(str) {
throw new Error("Not implemented");
return str.toUpperCase();
}

/**
Expand All @@ -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 = {
Expand Down
Loading