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

core-js-101 #57

Open
wants to merge 1 commit into
base: master
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
146 changes: 109 additions & 37 deletions src/01-strings-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* *
******************************************************************************************* */


/**
* Returns the result of concatenation of two strings.
*
Expand All @@ -18,11 +17,10 @@
* 'aa','' => 'aa'
* '', 'bb' => 'bb'
*/
function concatenateStrings(/* value1, value2 */) {
throw new Error('Not implemented');
function concatenateStrings(value1, value2) {
return `${value1}${value2}`;
}


/**
* Returns the length of given string.
*
Expand All @@ -34,8 +32,8 @@ function concatenateStrings(/* value1, value2 */) {
* 'b' => 1
* '' => 0
*/
function getStringLength(/* value */) {
throw new Error('Not implemented');
function getStringLength(value) {
return value.length;
}

/**
Expand All @@ -51,8 +49,8 @@ function getStringLength(/* value */) {
* 'John','Doe' => 'Hello, John Doe!'
* 'Chuck','Norris' => 'Hello, Chuck Norris!'
*/
function getStringFromTemplate(/* firstName, lastName */) {
throw new Error('Not implemented');
function getStringFromTemplate(firstName, lastName) {
return `Hello, ${firstName} ${lastName}!`;
}

/**
Expand All @@ -65,11 +63,10 @@ function getStringFromTemplate(/* firstName, lastName */) {
* 'Hello, John Doe!' => 'John Doe'
* 'Hello, Chuck Norris!' => 'Chuck Norris'
*/
function extractNameFromTemplate(/* value */) {
throw new Error('Not implemented');
function extractNameFromTemplate(value) {
return value.slice(7, -1);
}


/**
* Returns a first char of the given string.
*
Expand All @@ -80,8 +77,8 @@ function extractNameFromTemplate(/* value */) {
* 'John Doe' => 'J'
* 'cat' => 'c'
*/
function getFirstChar(/* value */) {
throw new Error('Not implemented');
function getFirstChar(value) {
return value[0];
}

/**
Expand All @@ -95,8 +92,8 @@ function getFirstChar(/* value */) {
* 'cat' => 'cat'
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(/* value */) {
throw new Error('Not implemented');
function removeLeadingAndTrailingWhitespaces(value) {
return value.trim();
}

/**
Expand All @@ -110,8 +107,8 @@ function removeLeadingAndTrailingWhitespaces(/* value */) {
* 'A', 5 => 'AAAAA'
* 'cat', 3 => 'catcatcat'
*/
function repeatString(/* value, count */) {
throw new Error('Not implemented');
function repeatString(value, count) {
return value.repeat(count);
}

/**
Expand All @@ -126,8 +123,8 @@ function repeatString(/* value, count */) {
* 'I like legends', 'end' => 'I like legs',
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(/* str, value */) {
throw new Error('Not implemented');
function removeFirstOccurrences(str, value) {
return str.replace(value, '');
}

/**
Expand All @@ -141,11 +138,10 @@ function removeFirstOccurrences(/* str, value */) {
* '<span>' => 'span'
* '<a>' => 'a'
*/
function unbracketTag(/* str */) {
throw new Error('Not implemented');
function unbracketTag(str) {
return str.slice(1, str.length - 1);
}


/**
* Converts all characters of the specified string into the upper case
*
Expand All @@ -156,8 +152,8 @@ function unbracketTag(/* str */) {
* 'Thunderstruck' => 'THUNDERSTRUCK'
* 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
*/
function convertToUpperCase(/* str */) {
throw new Error('Not implemented');
function convertToUpperCase(str) {
return str.toUpperCase();
}

/**
Expand All @@ -175,8 +171,8 @@ function convertToUpperCase(/* str */) {
* ],
* 'info@gmail.com' => ['info@gmail.com']
*/
function extractEmails(/* str */) {
throw new Error('Not implemented');
function extractEmails(str) {
return str.split(';');
}

/**
Expand All @@ -202,10 +198,21 @@ function extractEmails(/* str */) {
* '└──────────┘\n'
*
*/
function getRectangleString(/* width, height */) {
throw new Error('Not implemented');
}
function getRectangleString(width, height) {
let res = '';

for (let i = 0; i < height; i += 1) {
if (i === 0) {
res += `┌${'─'.repeat(width - 2)}┐\n`;
} else if (i === height - 1) {
res += `└${'─'.repeat(width - 2)}┘\n`;
} else {
res += `│${' '.repeat(width - 2)}│\n`;
}
}

return res;
}

/**
* Encode specified string with ROT13 cipher
Expand All @@ -223,8 +230,20 @@ function getRectangleString(/* width, height */) {
* => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
*
*/
function encodeToRot13(/* str */) {
throw new Error('Not implemented');
function encodeToRot13(str) {
const charOne = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const charTwo = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
let result = '';
let index = 0;
for (let i = 0; i < str.length; i += 1) {
if (str[i] === ' ' || str[i] === '?' || str[i] === '!') {
result += str[i];
} else {
index = charOne.indexOf(str[i]);
result += charTwo[index];
}
}
return result;
}

/**
Expand All @@ -240,11 +259,10 @@ function encodeToRot13(/* str */) {
* isString('test') => true
* isString(new String('test')) => true
*/
function isString(/* value */) {
throw new Error('Not implemented');
function isString(value) {
return value instanceof String || typeof value === 'string';
}


/**
* Returns playid card id.
*
Expand All @@ -269,10 +287,64 @@ function isString(/* value */) {
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(/* value */) {
throw new Error('Not implemented');
}
function getCardId(value) {
const array = [
'A♣',
'2♣',
'3♣',
'4♣',
'5♣',
'6♣',
'7♣',
'8♣',
'9♣',
'10♣',
'J♣',
'Q♣',
'K♣',
'A♦',
'2♦',
'3♦',
'4♦',
'5♦',
'6♦',
'7♦',
'8♦',
'9♦',
'10♦',
'J♦',
'Q♦',
'K♦',
'A♥',
'2♥',
'3♥',
'4♥',
'5♥',
'6♥',
'7♥',
'8♥',
'9♥',
'10♥',
'J♥',
'Q♥',
'K♥',
'A♠',
'2♠',
'3♠',
'4♠',
'5♠',
'6♠',
'7♠',
'8♠',
'9♠',
'10♠',
'J♠',
'Q♠',
'K♠',
];

return array.indexOf(value);
}

module.exports = {
concatenateStrings,
Expand Down
Loading