JS Exercises
JS Exercises
Loops
Problems for exercise and homework for the "Technology Fundamentals" course @ SoftUni.
Submit your solutions in the SoftUni judge system at:
https://judge.softuni.bg/Contests/1207/Intro-and-Basic-Syntax-Exercise
1. Ages
Write a function that determines whether based on the given age a person is: baby, child, teenager,
adult, elder. The input comes as single number parameter. The bounders are:
0-2 – baby;
3-13 – child;
14-19 – teenager;
20-65 – adult;
>=66 – elder;
All the values are inclusive.
Examples
Input Output
20 adult
1 baby
100 elder
2. Rounding
Write a JS function that rounds numbers to specific precision.
The input comes as two numbers. The first value is the number to be rounded and the second is the
precision (significant decimal places). If a precision is passed, that is more than 15 it should
automatically be reduced to 15.
Remove trailing zeroes, if any (you can use parseFloat())
The output should be printed to the console. Do not print insignificant decimals.
Examples
Input Output Input Output
3.141592653589793238462 3.14 10.5,3 10.5
6433832795,2
3. Division
You will be given a number and you have to return whether that number is divisible by the following
numbers: 2, 3, 6, 7, and 10. You should always take the bigger division. If the number is divisible by
both 2 and 3 it is also divisible by 6 and you should print only the division by 6. If a number is divisible by
2 it is sometimes also divisible by 10 and you should print the division by 10. If the number is not
divisible by any of the given numbers print "Not divisible". Otherwise print "The number is divisible by
{number}".
Examples
Input Output
4. Vacation
You are given a group of people, type of the group, and day of the week they are going to stay. Based
on that information calculate how much they have to pay and print that price on the console. Use the
table below. In each cell is the price for a single person. The output should look like that:
"Total price: {price}". The price should be formatted to the second decimal point.
Regular 15 20 22.50
· St
udents – if the group is bigger than or equal to 30 people you should reduce the total price by
15%
· Bu
siness – if the group is bigger than or equal to 100 people 10 of them can stay for free.
· Re
gular – if the group is bigger than or equal 10 and less than or equal to 20 reduce the total price
by 5%
Examples
Input Output
30, Total price: 266.73
"Students",
"Sunday"
40, Total price: 800.00
"Regular",
"Saturday"
5. Leap Year
Write a JS function to check whether a year is leap. Leap years are either divisible by 4 but not by 100 or
are divisible by 400. Return the result like examples below:
Examples
Input Output
1984 yes
2003 no
4 yes
Examples
Input Output
5, 10 5 6 7 8 9 10
Sum: 45
0, 26 0 1 2 … 26
Sum: 351
50, 60 50 51 52 53 54 55 56 57 58 59 60
Sum: 605
4. Triangle of Numbers
Write a function, which receives a single number – n, and prints a triangle from 1 to n as in the
examples.
Constraints
n will be in the interval [1...20].
Examples
Input Output Input Output Input Output
3 1 5 1 6 1
2 2 2 2 2 2
3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6
5. Multiplication Table
You will receive a number as an input from the console. Print the 10 times table for this number. See
the examples below for more information.
Output
Print every row of the table in the following format:
Constraints
The number will be an integer will be in the interval [1…100]
Examples
Input Output Input Output
5 5 X 1 = 5 2 2 X 1 = 2
5 X 2 = 10 2 X 2 = 4
5 X 3 = 15 2 X 3 = 6
5 X 4 = 20 2 X 4 = 8
5 X 5 = 25 2 X 5 = 10
5 X 6 = 30 2 X 6 = 12
5 X 7 = 35 2 X 7 = 14
5 X 8 = 40 2 X 8 = 16
5 X 9 = 45 2 X 9 = 18
5 X 10 = 50 2 X 10 = 20
6. * Login
You will be given a string representing a username. The password will be that username reversed. Until
you receive the correct password print on the console "Incorrect password. Try again.". When you
receive the correct password print "User {username} logged in." However on the fourth try if the
password is still not correct print "User {username} blocked!" and end the program. The input comes as
an array of strings.
Examples
Input Output
['Acer','login','go','let me in','recA'] Incorrect password. Try again.
Incorrect password. Try again.
Incorrect password. Try again.
User Acer logged in.
The pyramid is built with 1x1 blocks with height equal to the given increment. The first step of the
pyramid has width and length equal to the given base and every next step is reduced by 2 blocks (1
from each side). The height of every step equals the given increment. See the drawing for an example.
White steps are covered in marble, blue steps are covered in lapis lazuli (every fifth layer from the
bottom), and yellow steps are made entirely out of gold (top-most step).
Since the outer layer of each step is made of a decorative material, to calculate the required stone for
one step, reduce the width and length by 2 blocks (one from each side), find it’s area and multiply it by
the increment. The rest of the step is made out of lapis lazuli for every fifth step from the bottom and
marble for all other steps. To find the amount needed, you may, for example, find its perimeter and
reduce it by 4 (to compensate for the overlapping corners) and multiply the result by the increment. See
the drawing for details (grey is stone, white is decoration).
Input
You will receive two number parameters base and increment.
Output
Print on the console on separate lines the total required amounts of each material rounded up and the
final height of the pyramid rounded down, as shown in the examples.
Constraints
The base will always be an integer greater than zero
The increment will always be a number greater than zero
Number.MAX_SAFE_INTEGER will never be exceeded for any of the calculations
Examples
Input Output Explanation
11, Stone required: 165 Step Size Stone Marble Lapis Gold
1 Marble required: 112
Lapis Lazuli required: 8 1st 11x11 81 40 - -
Gold required: 1
Final pyramid height: 6 2nd 9x9 49 32 - -
3rd 7x7 25 24 - -
4th 5x5 9 16 - -
5th 3x3 1 - 8 -
6th 1x1 - - - 1
Input
You will receive an array of numbers, representing your shift at the mine.
Output
Print on the console these lines in the following formats:
Constraints
The input array may contain up to 1,000 elements
The numbers in the array are in range [0.01..5,000.00] inclusive
Allowed time/memory: 100ms/16MB
Examples
Input Output
[100,200,300] Bought bitcoins: 2
Day of the first purchased bitcoin: 2
Left money: 10531.78 lv.
Scroll down to see the explanation for the first example and more examples.
Explanation
Day 1 – you dig up 100 g of gold then exchange it for 6751.00 lv.
Day 2 – you dig up 200 g of gold then exchange it for 13,502.00 lv. and the total amount of money is 20,253.00 lv.
Then you buy 1 Bitcoin which leaves you with 8,303.84 lv. Also this purchase is the first day you bought bitcoin.
Day 3 – you dig up 300 g of gold but then 30% of it is stolen and your gold drops to 210 g which you exchange for
14,177.10 lv. making your total amount of money 22,480.94 lv. Then you buy 1 Bitcoin making the final amount of
money that you are left with 10,531.78 lv. with 2 bought Bitcoins.