Skip to content

cootook/basic-js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BasicJS

⚠️ DO NOT SUBMIT PULL REQUESTS TO THIS REPO ⚠️

General task description

Your task is to write several functions that are the solution to the subtasks. Subtasks descriptions, as well as instructions on how to run tests and submit solutions are below.




Chain maker

Let's practice in chaining!

Your task is to create the object chainMaker that creates chains. The finished chain is a string and looks like this: '( value1 )~~( value2 )~~( value3 )'. The chainMaker has several methods for creating chains and modifying them:

  • getLength returns the current chain length as a number;
  • addLink(value) adds a link containing a string representation of the value to the chain;
  • removeLink(position) removes a chain link in the specified position;
  • reverseChain reverses the chain;
  • finishChain ends the chain and returns it.

addLink, reverseChain and removeLink methods are chainable, while the another ones are not. If addLink is called with no arguments, it adds an empty link ('( )') to the chain. If removeLink accepts invalid position (e.g. not a number, or a fractional number, or corresponding to a nonexistent link), it must throw an Error. After calling the finishChain method, the existing chain must be deleted, as if an Error was thrown.

For example:

chainMaker.addLink(1).addLink(2).addLink(3).finishChain() => '( 1 )~~( 2 )~~( 3 )'

chainMaker.addLink(1).addLink(2).removeLink(1).addLink(3).finishChain() => '( 2 )~~( 3 )'

chainMaker.addLink(1).addLink(2).reverseChain().addLink(3).finishChain() => '( 2 )~~( 1 )~~( 3 )'

Write your code in src/simple-chain.js.


Recursive depth calculator

Go deeper

Your task is to implement the class DepthCalculator with method calculateDepth that takes an array and returns its depth.

calculateDepth method must pass the given array recursively. Depth of a flat array is 1. Method must correctly work with arrays that contain no elements or contain empty arrays.

For example:

const depthCalc = new DepthCalculator(); const { calculateDepth } = depthCalc;

calculateDepth([1, 2, 3, 4, 5]) => 1

calculateDepth([1, 2, 3, [4, 5]]) => 2

calculateDepth([[[]]]) => 3

Write your code in src/recursive-depth.js.


Extended repeater

Your task is to implement the function repeater(str, options). This function returns a repeating string based on the given parameters:

  • str is a string to repeat;
  • options is an object of options, that contains properties:
    • repeatTimes sets the number of repetitions of the str;
    • separator is a string separating repetitions of the str;
    • addition is an additional string that will be added to each repetition of the str;
    • additionRepeatTimes sets the number of repetitions of the addition;
    • additionSeparator is a string separating repetitions of the addition.

The str and addition parameters are strings by default. In case when type of these parameters is different, they must be converted to a string.

separator and additionSeparator parameters are strings.

repeatTimes and additionRepeatTimes are integer numbers (in the absence of any of them, the corresponding string is not repeated).

The only indispensable parameter is str, any others may be not defined. separator default value is '+'. additionSeparator default value is '|'.

For example: repeater('STRING', { repeatTimes: 3, separator: '**', addition: 'PLUS', additionRepeatTimes: 3, additionSeparator: '00' }) => 'STRINGPLUS00PLUS00PLUS**STRINGPLUS00PLUS00PLUS**STRINGPLUS00PLUS00PLUS'

Write your code in src/extended-repeater.js.


Vigenere cipher

Cryptography is awesome! Let's try to organize production of encryption machines. Our machines will use one of the encryption methods that are easy to understand, but also not amenable to simple cryptanalysis - the Vigenere cipher.

Our machine will have 2 modifications: direct and reverse (the type of machine is determined at the moment of creation). The direct machine simply encodes and decodes the string that was transmitted to it, and the reverse machine returns an inverted string after encoding and decoding.

Your task is to implement the class VigenereCipheringMachine. constructor of this class accepts true (or nothing) to create direct machine and false to create reverse machine. Each instance of VigenereCipheringMachine must have 2 methods: encrypt and decrypt.

encrypt method accepts 2 parameters: message (string to encode) and key (string-keyword).

decrypt method accepts 2 parameters: encryptedMessage (string to decode) and key (string-keyword).

These parameters for both methods are mandatory. If at least one of them has not been given, an Error must be thrown. The text returned by these methods must be uppercase. Machines encrypt and decrypt only latin alphabet (all other symbols remain unchanged).

You don't need to validate value sent to constructor and to encrypt and decrypt methods (except throwing an Error on absence of argument for these methods).

For example:

const directMachine = new VigenereCipheringMachine();

const reverseMachine = new VigenereCipheringMachine(false);

directMachine.encrypt('attack at dawn!', 'alphonse') => 'AEIHQX SX DLLU!'

directMachine.decrypt('AEIHQX SX DLLU!', 'alphonse') => 'ATTACK AT DAWN!'

reverseMachine.encrypt('attack at dawn!', 'alphonse') => '!ULLD XS XQHIEA'

reverseMachine.decrypt('AEIHQX SX DLLU!', 'alphonse') => '!NWAD TA KCATTA'

Write your code in src/vigenere-cipher.js.


Prerequisites

  1. Install Node.js
  2. Fork this repository: https://github.com/AlreadyBored/basic-js
  3. Clone your newly created repo: https://github.com/<%your_github_username%>/basic-js/
  4. Go to folder basic-js
  5. To install all dependencies use npm install
  6. Run npm run test in command line.
  7. You will see the number of pending, passing and failing tests. 100% of passing tests is equal to 100p in score

Submit to rs app

  1. Open rs app and login
  2. Go to submit task page
  3. Select your task (BasicJS)
  4. Press the submit button and enjoy

Notes

  1. We recommend you to use nodejs of version 12 or lower. If you use any of features, that does not supported by node v12, there may be problems with task submit.
  2. Please, be sure that each of your tests is limited to 30 sec.

© AlreadyBored

& Thanks mikhama for assistance!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%