Skip to content

Commit f7ca617

Browse files
committed
Added code for a few questions about strings, arrays, fizzbuzz, etc.
1 parent 58bd2c6 commit f7ca617

File tree

4 files changed

+283
-0
lines changed

4 files changed

+283
-0
lines changed

fizzbuzz.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5
2+
3+
let fizzbuzz = () => {
4+
for (let i = 0; i <= 100; i++) {
5+
6+
let output = "";
7+
8+
if (i % 3 === 0) {
9+
output = "fizz";
10+
}
11+
12+
if (i % 5 === 0) {
13+
output += "buzz";
14+
}
15+
16+
(output) ? console.log(output) : console.log(i);
17+
}
18+
}
19+
20+
fizzbuzz();

stringsArrays.js

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
//return first non-repeating char - O notation = O(2n) so O(n)
2+
const firstNonRepeat = (string) => {
3+
let charCount = {};
4+
5+
if (!string) {
6+
return "No string input.";
7+
}
8+
9+
for (let i = 0; i < string.length; i++) {
10+
let char = string[i];
11+
if (charCount[char]) {
12+
charCount[char]++;
13+
} else {
14+
charCount[char] = 1;
15+
}
16+
}
17+
18+
for (let j = 0; j < string.length; j++) {
19+
let charChecking = string[j];
20+
if (charCount[charChecking] == 1) {
21+
return string.charAt(j);
22+
}
23+
}
24+
25+
return "No non-repeating characters."
26+
}
27+
28+
console.log(`Returns first non-repeating character: ${firstNonRepeat("ararta")}`);
29+
30+
/***********************************/
31+
//removes all characters in a specified string from another string
32+
//O(n*m) - likely O(n) because m<n
33+
const removeChars = (string, remove) => {
34+
let newString = "";
35+
36+
for (let i = 0; i < string.length; i++) {
37+
let deleteMe = false;
38+
39+
for (let j = 0; j < remove.length; j++) {
40+
if (remove.charAt(j).toLowerCase() == string.charAt(i).toLowerCase()) {
41+
deleteMe = true;
42+
}
43+
}
44+
if (!deleteMe) {
45+
newString = newString.concat(string[i]);
46+
};
47+
}
48+
49+
return newString;
50+
}
51+
52+
console.log(`Removes all characters in a specified string from another string: ${removeChars("yaywowdogewow", "wow")}`);
53+
54+
//more efficient way O(n) This uses a hash table!
55+
const otherRemoveChars = (input, remove) => {
56+
let r = {};
57+
let newString = '';
58+
59+
//r[c] is creating a property c (which is the characters of remove) that holds a boolean.
60+
remove.split('').forEach(c => r[c] = true);
61+
//now when you go through input you'll be able to check quickly if r has a property of that character that has a boolean. If it does, then you don't copy it.
62+
input.split('').forEach((c) => {
63+
if (typeof r[c] === 'undefined') {
64+
newString += c;
65+
}
66+
});
67+
68+
return newString;
69+
}
70+
71+
console.log(`Removes all characters in a specified string from another string (more efficient): ${otherRemoveChars("yaywowdogewow", "wow")}`);
72+
73+
74+
//chris' soooooper efficient way
75+
const otherRemoveChars2 = (input, remove) => {
76+
let r = {};
77+
78+
remove.split('').forEach(c => r[c] = true);
79+
return input.split('')
80+
.filter(c => typeof r[c] === 'undefined')
81+
.reduce((t, c) => {
82+
return t += c
83+
}, '');
84+
}
85+
86+
console.log(`Removes all characters in a specified string from another string (Chris' way): ${otherRemoveChars2("yaywowdogewow", "wow")}`);
87+
88+
/***********************************/
89+
//reverse the words!
90+
const reverseEasy = (string) => {
91+
92+
let stringArr = string.split(" ");
93+
94+
let reverseStr = stringArr.reverse().join(" ");
95+
return reverseStr;
96+
}
97+
98+
console.log(`Reverse dem words (easy way): ${reverseEasy("i am ameiiizing")}`)
99+
100+
const reverseHard = (string) => {
101+
let reverseArr = [];
102+
let wordLength = 0;
103+
104+
for (let i = string.length; i >= 0; --i) {
105+
if (string[i] == " ") {
106+
reverseArr.push(string.substr(i + 1, wordLength));
107+
wordLength = 0;
108+
} else if (i == 0) {
109+
reverseArr.push(string.substr(i, wordLength + 1));
110+
} else {
111+
wordLength++;
112+
}
113+
}
114+
return reverseArr.join(" ");
115+
}
116+
117+
console.log(`Reverse dem words (harder way): ${reverseHard("i am ameiiizing")}`)
118+
119+
/***********************************/
120+
//turn a string of numbers to an int
121+
const stringToInt = (string) => {
122+
let newInt = 0;
123+
let digitCount = 0;
124+
let numberIsNeg = false;
125+
126+
let i = 0;
127+
128+
if (string.charAt(i) == "-") {
129+
numberIsNeg = true;
130+
i = 1;
131+
}
132+
133+
//- "0" is suppose to make the string a number, but in javascript it isn't really necessary
134+
for (i; i < string.length; i++) {
135+
let numberInt = (string.charAt(i) - "0") * Math.pow(10, (string.length - i - 1));
136+
newInt = newInt + numberInt;
137+
}
138+
139+
if (numberIsNeg) {
140+
newInt *= -1;
141+
}
142+
143+
return newInt;
144+
}
145+
console.log(`String to int: ${typeof stringToInt("1234")}`)
146+
147+
/***********************************/
148+
//turn an int to a string of numbers
149+
150+
const intToString = (int) => {
151+
let newStr = "";
152+
let digitCount = 0;
153+
let numberIsNeg = false;
154+
let temp = [];
155+
156+
let i = 0;
157+
if (int < 0) {
158+
numberIsNeg = true;
159+
int *= -1;
160+
}
161+
162+
if(int==0) {
163+
return "0";
164+
}
165+
166+
while (int != 0) {
167+
temp[i] = (Math.floor(int % 10));
168+
int = (Math.floor(int / 10));
169+
i++
170+
}
171+
172+
for (i; i > 0; i--) {
173+
newStr += temp[i-1];
174+
}
175+
176+
if (numberIsNeg) {
177+
newStr = "-" + newStr;
178+
}
179+
180+
return newStr;
181+
}
182+
console.log(`Int to String: ${typeof intToString("1234")}`)
183+
184+
/***********************************/
185+
// Make a function that duplicates an array:
186+
187+
//One Way:
188+
let duplicate = (nums) => {
189+
nums.forEach((value) => {
190+
nums.push(value);
191+
})
192+
193+
return nums;
194+
}
195+
196+
console.log(duplicate([1, 2, 3, 4, 5])); // [1,2,3,4,5,1,2,3,4,5]
197+
198+
//Second way:
199+
let duplicate2 = (arr) => {
200+
return arr.concat(arr);
201+
};
202+
203+
console.log(duplicate2([1, 2, 3, 4, 5])); // [1,2,3,4,5,1,2,3,4,5]

substringCounter.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*Given a string and a non-empty substring, compute the number of times that substring appears in the string, without the sub strings overlapping.
2+
strCount("catcowcat", "cat") → 2 strCount("catcowcat", "cow") → 1 strCount("catcowcat", "dog") → 0
3+
*/
4+
5+
//iterative
6+
let strCount = (string, substr) => {
7+
8+
let matchCount = 0;
9+
for (let i = 0; i < string.length - 1; i++) {
10+
if (string.charAt(i) === substr.charAt(0)) {
11+
let matchChar = 0;
12+
let stringChar = i;
13+
for (let j = 0; j < substr.length; j++) {
14+
if (string.charAt(stringChar) === substr.charAt(j)) {
15+
matchChar++;
16+
stringChar++;
17+
18+
if (matchChar === substr.length) {
19+
matchCount++;
20+
stringChar = 0;
21+
matchChar = 0;
22+
break;
23+
}
24+
} else {
25+
matchChar = 0;
26+
stringChar=0;
27+
break;
28+
}
29+
}
30+
} else {
31+
continue;
32+
}
33+
}
34+
35+
return matchCount === 0 ? "No match found" : matchCount;
36+
}
37+
38+
//really short way of doing it using split
39+
shortStrCount = (str, subStr) => str.split(subStr).length - 1;
40+
41+
console.log(`# of times substring is in there:" ${strCount("catmoocatcatca","cat")}`);
42+
console.log(`# of times substring is in there (easy way):" ${shortStrCount("catmoocatcatca","cat")}`);
43+

sum.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*How would you make this work?
2+
add(2, 5); // 7
3+
add(2)(5); // 7
4+
*/
5+
6+
let add = (a, b) =>{
7+
if (a & b){
8+
return a+b;
9+
} else {
10+
return function (c){
11+
return a + c;
12+
}
13+
}
14+
}
15+
16+
console.log(`add(2,3): ${add(2,3)}`);
17+
console.log(`add(2)(3213): ${add(2)(3213)}`);

0 commit comments

Comments
 (0)