-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS_HW_2.js
More file actions
88 lines (71 loc) · 2.34 KB
/
Copy pathJS_HW_2.js
File metadata and controls
88 lines (71 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//1. Write a script that will calculate and display the result of raising 2 to the power of 10, starting from the power of 1
{
let i = 2;
for (j = 1; j <= 10; j++) {
console.log(i ** j);
}
//1*. Convert 1 task into a function that takes as input the power to which the number 2 will be raised
function func(x) {
let i = 2;
for (j = 1; j <= x; j++) {
console.log(i ** j);
}
}
func(6);
}
//2.Write a script that will print 5 lines to the console in such a way that :) is displayed on the first line, :) :) on the second line, and so on
//Console example:
//:)
//:):)
//:):):)
//:):):):)
//:):):):):)
let i = ':)';
for (j = 1; j <= 5; j++) {
console.log(i.repeat(j));
}
//2*. Convert task 2 into a function that takes a string as input, which will be output to the console (as in the smiley condition), as well as the number of lines to output
//e.g. function printSmile(line, numberOfRows)
function printSmile(line, numberOfRows) {
for (j = 1; j <= numberOfRows; j++) {
console.log(line.repeat(j));
}
}
printSmile(':)', 5);
//3**. Write a function that takes a word as input. The task of the function is to calculate and output to the console how many vowels and how many consonants are in the word.
//e.g. function getWordStructure(word)
//In console:
//The word (word) consists of (number) vowels and (number) consonants
//Checks: 'case', 'Case', 'Check-list'
function getWordStructure(word) {
let w = word.toLowerCase().split('');
let vowel = 'aeiou';
let cons = 'bcdfghjklmnpqrstvwxyz';
let vowelСount = 0;
let consonantСount = 0;
for (let x = 0; x < w.length; x++) {
if (vowel.indexOf(w[x]) !== -1) {
vowelСount++;
}
if (cons.indexOf(w[x]) !== -1) {
consonantСount++;
}
}
return (`The word '${word}' consists of ${vowelСount} vowels and ${consonantСount} consonants`);
}
console.log(getWordStructure('case'));
console.log(getWordStructure('Case'));
console.log(getWordStructure('Check-list'));
//4**. Write a function that checks if a word is a palindrome
//e.g. function isPalindrom(word)
//Checks: 'abba', 'Abba'
function isPalindrom_1(word) {
let w = word.toLowerCase().replace(/[^a-zA-Z0-9]+/i)
for (let i = 0, j = w.length - 1; i < j; i++, j--) {
if (w[i] != w[j]) {
return false;
}
} return true;
}
console.log(isPalindrom_1('abba'));
console.log(isPalindrom_1('Abba'));