-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice2.js
More file actions
104 lines (93 loc) · 2.23 KB
/
Copy pathpractice2.js
File metadata and controls
104 lines (93 loc) · 2.23 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//HOW DO YOU REVERSE A GIVEN STRING STRING IN PLACE
function reverseString(str){
if (typeof str !=='string' || str ===''){
return null;
}
return str.split('').reverse().join('')
}
//2.HOW DO YOU PRINT DUPLICATE CHARACTERS FROM A STRING.
function duplicateChar(str){
if (typeof str !=='string' || str ===''){
return null;
}
let charCount = {};
let duplicateCount = ''
for(let char of str){
if(charCount[char]){
duplicateCount += char
}else{
charCount[char] =1;
}
}
return duplicateChar;
}
function duplicateChar (str){
if (typeof str !=='string' || str ===''){
return null;
}
let charCount = new Set();
let duplicateCount = [];
for(let char of str){
if(charCount.has(char)){
duplicateCount.push(char)
}else{
charCount.add(char)
}
}
return duplicateChar;
}
//HOW DO YOU CHECK IF TWO STRINGS ARE ANAGRAMS OF EACH OTHERS
function isAnagram(str1, str2){
if (typeof str1 !=='string' || str1 ==='' || typeof str2 !=='string' || str2 ===''){
return null;
}
for(let char of str1){
charCount1[char] =(charCount1[char] || 0) + 1;
}
for(let char of str2){
charCount2[char] =(charCount2[char] || 0) + 1;
}
for(let char in charCount1){
if(charCount1[char] !== charCount2[char]){
return false;
}
}
return true;
}
let vowel = 'aeiouAEIOU'
let vowelsCount = 0;
let consonantCount = 0;
for (let char of str){
if(vowel.includes(char)){
vowelcount++
}else{
if(char <='a' && char >='z'|| char <='A' && char >='Z'){
consonantCount++
}
} return{
vowels: vowelsCount,
consonants: consonantCount
}
}
let count =0;
for(c of char){
if(c === char){
count++
}
return count
}
str.split('').filter(c => c === char).length;
let charcount1 = {};
let charCount2 = {};
for(let char of str1){
charcount1[char]= (charcount1[char] || 0) + 1;
}
for(let char of str2){
charCount2[char]= (charCount2[char] || 0) + 1
}
for(let char in charcount1){
if(charcount1[char] !== charCount2[char]){
return false;
}
return true;
}