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

done #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

done #54

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
3 changes: 3 additions & 0 deletions jest-html-reporters-attach/test/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jest-html-reporters-attach/test/result.js

Large diffs are not rendered by default.

35 changes: 23 additions & 12 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
* ['Array', 'Number', 'string'], 'Date' => -1
* [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;
}

/**
* Returns the doubled array - elements of the specified array are repeated twice
Expand All @@ -26,9 +32,10 @@ function findElement(arr, value) {
* [0, 1, 2, 3, 4, 5] => [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
* [] => []
*/

function doubleArray(arr) {
throw new Error("Not implemented");
}
return arr.concat(arr);
}

/**
* Returns an array of positive numbers from the specified array in original order
Expand All @@ -41,9 +48,10 @@ function doubleArray(arr) {
* [-1, 2, -5, -4, 0] => [ 2 ]
* [] => []
*/

function getArrayOfPositives(arr) {
throw new Error("Not implemented");
}
return arr.filter(num => num > 0);
}

/**
* Removes falsy values from the specified array
Expand All @@ -58,9 +66,10 @@ function getArrayOfPositives(arr) {
* [ 1, 2, 3, 4, 5, 'false' ] => [ 1, 2, 3, 4, 5, 'false' ]
* [ false, 0, NaN, '', undefined ] => [ ]
*/

function removeFalsyValues(arr) {
throw new Error("Not implemented");
}
return arr.filter(item => !!item);
}

/**
* Returns the array of string lengths from the specified string array.
Expand All @@ -72,9 +81,10 @@ function removeFalsyValues(arr) {
* [ '', 'a', 'bc', 'def', 'ghij' ] => [ 0, 1, 2, 3, 4 ]
* [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ]
*/

function getStringsLength(arr) {
throw new Error("Not implemented");
}
return arr.map(str => str.length);
}

/**
* Returns the sum of all items in the specified array of numbers
Expand All @@ -88,9 +98,10 @@ function getStringsLength(arr) {
* [ -1, 1, -1, 1 ] => 0
* [ 1, 10, 100, 1000 ] => 1111
*/

function getItemsSum(arr) {
throw new Error("Not implemented");
}
return arr.reduce((sum, num) => sum + num, 0);
}

module.exports = {
findElement,
Expand Down
20 changes: 17 additions & 3 deletions src/bulb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ const BULB_OFF_URL = "https://i.postimg.cc/KjK1wL3c/bulb-off.png";
TODO:
Fetch the status of the 'flexSwitchCheckChecked' checkbox and call the appropiate function to turn the bulb on or off
*/
function lightSwitch() {}

function lightSwitch() {
const checkbox = document.getElementById('flexSwitchCheckChecked');
const bulbImage = document.getElementById('bulb');

if (checkbox.checked) {
bulb_on(bulbImage);
} else {
bulb_off(bulbImage);
}
}

/*
TODO:
Set the "bulb" element's Image src to be the image specified by BULB_ON_URL
*/
function bulb_on() {}
function bulb_on(bulbImage) {
bulbImage.src = BULB_ON_URL;
}

/*
TODO:
Set the "bulb" element's Image src to be the image specified by BULB_OFF_URL
*/
function bulb_off() {}
function bulb_off(bulbImage) {
bulbImage.src = BULB_OFF_URL;
}
111 changes: 95 additions & 16 deletions src/conditionalAndLoops.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
* 21 => 'Fizz'
*
*/

function getFizzBuzz(num) {
throw new Error("Not implemented");
}
if (num % 3 === 0 && num % 5 === 0) {
return 'FizzBuzz';
} else if (num % 3 === 0) {
return 'Fizz';
} else if (num % 5 === 0) {
return 'Buzz';
} else {
return num;
}
}

/**
* Returns the factorial of the specified integer n.
Expand All @@ -33,9 +42,14 @@ function getFizzBuzz(num) {
* 5 => 120
* 10 => 3628800
*/

function getFactorial(n) {
throw new Error("Not implemented");
}
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

/**
* Returns the sum of integer numbers between n1 and n2 (inclusive).
Expand All @@ -49,9 +63,16 @@ function getFactorial(n) {
* 5,10 => 45 ( = 5+6+7+8+9+10 )
* -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 += i;
}

return sum;
}

/**
* Returns true, if a triangle can be built with the specified sides a,b,c and false
Expand All @@ -68,9 +89,10 @@ function getSumBetweenNumbers(n1, n2) {
* 10,1,1 => false
* 10,10,10 => true
*/

function isTriangle(a, b, c) {
throw new Error("Not implemented");
}
return a + b > c && a + c > b && b + c > a;
}

/**
* Reverse the specified string (put all chars in reverse order)
Expand All @@ -84,9 +106,10 @@ function isTriangle(a, b, c) {
* 'rotator' => 'rotator'
* 'noon' => 'noon'
*/

function reverseString(str) {
throw new Error("Not implemented");
}
return str.split('').reverse().join('');
}

/**
* Returns true if the specified string has the balanced brackets and false otherwise.
Expand All @@ -109,9 +132,30 @@ function reverseString(str) {
* '{)' = false
* '{[(<{[]}>)]}' = true
*/

function isBracketsBalanced(str) {
throw new Error("Not implemented");
}
const stack = [];
const openingBrackets = '([{<';
const closingBrackets = ')]}>';
const bracketPairs = {
')': '(',
']': '[',
'}': '{',
'>': '<',
};

for (const char of str) {
if (openingBrackets.includes(char)) {
stack.push(char);
} else if (closingBrackets.includes(char)) {
if (stack.length === 0 || stack.pop() !== bracketPairs[char]) {
return false;
}
}
}

return stack.length === 0;
}

/**
* Returns the human readable string of time period specified by the start and end time.
Expand Down Expand Up @@ -144,9 +188,43 @@ function isBracketsBalanced(str) {
* Date('2000-01-01 01:00:00.100'), Date('2015-01-02 03:00:05.000') => '15 years ago'
*
*/

function timespanToHumanString(startDate, endDate) {
throw new Error("Not implemented");
}
const diffInMillis = endDate - startDate;
const diffInSeconds = Math.floor(diffInMillis / 1000);
const diffInMinutes = Math.floor(diffInSeconds / 60);
const diffInHours = Math.floor(diffInMinutes / 60);
const diffInDays = Math.floor(diffInHours / 24);
const diffInMonths = Math.floor(diffInDays / 30);
const diffInYears = Math.floor(diffInDays / 365);

if (diffInSeconds <= 45) {
return 'a few seconds ago';
} else if (diffInSeconds <= 90) {
return 'a minute ago';
} else if (diffInMinutes <= 45) {
return `${diffInMinutes} minutes ago`;
} else if (diffInMinutes <= 90) {
return 'an hour ago';
} else if (diffInHours <= 22) {
return `${diffInHours} hours ago`;
} else if (diffInHours <= 36) {
return 'a day ago';
} else if (diffInDays <= 25) {
return `${diffInDays} days ago`;
} else if (diffInDays <= 45) {
return 'a month ago';
} else if (diffInDays <= 345) {
return `${diffInMonths} months ago`;
} else if (diffInDays <= 545) {
return 'a year ago';
} else {
return `${diffInYears} years ago`;
}
}




/**
* Returns the string with n-ary (binary, ternary, etc, where n<=10) representation of
Expand All @@ -168,9 +246,10 @@ function timespanToHumanString(startDate, endDate) {
* 365, 4 => '11231'
* 365, 10 => '365'
*/

function toNaryString(num, n) {
throw new Error("Not implemented");
}
return num.toString(n);
}

module.exports = {
getFizzBuzz,
Expand Down
45 changes: 33 additions & 12 deletions src/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
* 5, 10 => 50
* 5, 5 => 25
*/

function getRectangleArea(width, height) {
throw new Error("Not implemented");
}
return width * height;
}

/**
* Returns an average of two given numbers.
Expand All @@ -26,8 +27,10 @@ function getRectangleArea(width, height) {
* -3, 3 => 0
*/
function getAverage(value1, value2) {
throw new Error("Not implemented");
}
return (value1 + value2) / 2;
}

// console.log(getAverage(6, 5))

/**
* Returns a root of linear equation a*x + b = 0 given by coefficients a and b.
Expand All @@ -42,8 +45,8 @@ function getAverage(value1, value2) {
* 5*x = 0 => 0
*/
function getLinearEquationRoot(a, b) {
throw new Error("Not implemented");
}
return -b / a;
}

/**
* Returns a last digit of a integer number.
Expand All @@ -58,8 +61,8 @@ function getLinearEquationRoot(a, b) {
* 0 => 0
*/
function getLastDigit(value) {
throw new Error("Not implemented");
}
return value % 10;
}

/**
* Returns a number by given string representation.
Expand All @@ -73,8 +76,8 @@ function getLastDigit(value) {
* '-525.5' => -525.5
*/
function parseNumberFromString(value) {
throw new Error("Not implemented");
}
return parseFloat(value);
}

/**
* Returns true is the number is prime; otherwise false.
Expand All @@ -94,8 +97,26 @@ function parseNumberFromString(value) {
* 17 => true
*/
function isPrime(n) {
throw new Error("Not implemented");
}
if (n <= 1) {
return false;
}

if (n <= 3) {
return true;
}

if (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 = {
getRectangleArea,
Expand Down
Loading