-
Notifications
You must be signed in to change notification settings - Fork 4
/
js_class_09_10_23
202 lines (138 loc) · 3.98 KB
/
js_class_09_10_23
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// THE CONST KEYWORD AND FUNCTION EXPRESSION
// let name = "abbey";
// name = "alaroye"
// console.log(name)
// const SURNAME = "olatunji"
//area = pi * r * r
// let radius = 5
// const PI = 3.142
// surname = "oladele"
// console.log(surname)
// function sum (a, b) {
// let res = a + b
// return res
// }
// sum(34, 6)
// const sum = function(c, d) {
// let res = c + d
// return res
// }
// console.log(sum())
// console.log(sum(34,19))
/*
const greeter = function (name) {
if(!name.startsWith("F")) {
// return ("Hi, " + name + "! " + name + " does not start with F")
return `Hi, ${name}! ${name} does not start with F`
}
else {
// return ("Good Morning, " + name)
return (`Good morning, ${name}`)
}
}
*/
// console.log(greeter("NuruDeen"))
// console.log("--------------")
// console.log(greeter("Feyi"))
let firstname = "ola"
let lastname = "ayinde"
let age = 34
// My name is Ola Ayinde and my age is 34.
// My firstname is Ola, my surname is Ayinde and my age is 34.
// 5. Write a JavaScript function that capitalizes the first letter of each word of a given string.
// Ex: "the quick brown fox"
/*
step1: Get the string to work on
2. iterate thorugh the string
3. identify the begining of every new word in the string
4. capitalize the first letter of every new word
*/
// let str = "the quick brown fox"
const capitalizer = function(str) {
//logic
let numOfSpace = 0
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) === " ") {
numOfSpace++
}
}
// console.log(counter)
let strWithEndSpace = str + " "
let newStr = ""
for (let i = 0; i <= numOfSpace; i++) {
newStr = newStr + strWithEndSpace[0].toUpperCase() + strWithEndSpace.slice(1, strWithEndSpace.indexOf(" ")) + " "
// console.log(newStr)
strWithEndSpace = strWithEndSpace.slice(strWithEndSpace.indexOf(" ")+1)
//console.log(str)
// console.log(newStr)
}
return newStr
}
// console.log(capitalizer("alameen just got back from ibadan with no bread, no wara, in fact he came with complaint."))
// Given two strings s and t, return true if t is an anagram of s,
//and false otherwise.
//An Anagram is a word or phrase formed by rearranging the letters
//of a different word or phrase, typically using all the original
//letters exactly once.
// // Example 1:
// Input: s = "anagram", t = "nagaram"
// Output: true
// Example 2:
// Input: s = "rat", t = "car"
// Output: false
const isAnagram = function(s, t) {
if(s.length !== t.length) {
return false
}
for (let i =0; i < s.length; i++) {
for (let j = 0; j < t.length; j++) {
if(s[i] === t[j]) {
// s2 = t.slice(j, j+1)
t= t.replace(t[j], "")
break;
}
}
}
// console.log(t)
if (t.length === 0) {
return true
}else {
return false
}
}
// console.log(isAnagram("silent", "listen"))
/*
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
s= "{}{()}{}(){}"
*/
const isValid = function (s) {
let pair = 0
if (s.length % 2 !== 0) {
return false
}
else {
for (let i = 0; i < s.length; i+=2) {
if ((s[i] == "[" && s[i+1] == "]") || (s[i] == "(" && s[i+1] == ")") || (s[i] == "{" && s[i+1] == "}")) {
pair++
// return true
}
}
if (pair === s.length/2) return true
else return false
}
}
// console.log(isValid("{}{()}{}(){}"))