JavaScript Tasks for Junior #1
Hello, friends!
Here you’ll find a few tasks which are helpful for beginners. If you have just started learning js or studying it for a year it’s a good challenge to solve these problems.
Don’t give up easily. You’d better try to solve this on your own. But if you have some problems I’ve placed the link with one of possible solutions after every exercise.
Enjoy!
Task 1. Dictionary with number of words
There’s a string that contains only letters and spaces.
What’s requiered? You should make a function getDictionary. In the function you can pass the string as an argument. Function returns an object with such a structure:
- Key — the word from the string (each one time)
- Value — the number of such words in the string
Example:
You can find the solution here
Task 2. Change and remove elements in an array
There’s an array. It has elements of three types:
- number
- string ‘change’
- string ‘remove’
What’s required? You should write a code which takes the original array and turns it into a new array constructed by following rules:
- numbers stay as they are
- elements ‘change’ turn into a random value from 0 to 10
- elements ‘remove’ should be removed from the array
Example:
You can find the solution here
Exersice 3. Create method forEach for an object
There’s an ‘array-like’ object — an object which has array-index property keys and property ‘length’ which equals the count of numbered properties.
Get GeniusProgrammer’s stories in your inbox
Join Medium for free to get updates from this writer.
An example of such an object: { 0: 'v1', 1: 'v2', length: 2 }
Note: simple object doesn’t contain an array methods like forEach
What’s required? You should create a method forEach in the object. It has to work like the forEach method of an array. It acceptes a function as an argument. The function has to be called for every numbered item of the object and have two parameters. One is an item of the object, the other is the index of the item.
One more thing, if you copy your forEach method from the object to another one, it should still work correctly in the context of the new object.
You can find the solution here
P.S.
I’m always glad to see intresting ways of solving the problems in comments if you have one.
Thaks for reading!





