Skip to content

Commit 05f81b2

Browse files
committed
javascript string problem solving
1 parent 36888c7 commit 05f81b2

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Interview-Questions/camelLetters.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Write a function that accepts a String as an argument
3+
4+
The function should capitalize ONLY every other letter in the String
5+
6+
The function should then return the converted String.
7+
8+
*/
9+
10+
// Edge cases
11+
/**
12+
* "" => ""
13+
* letter === character
14+
*
15+
* starting caps at letter index 0
16+
*
17+
* "hello" => "HeLlO"
18+
* "yo eli" => "Yo eLi"
19+
* "hello????" => "HeLlO????"
20+
* "HELLO" => "HeLlO"
21+
*/
22+
23+
const camelLetters = (str) => {
24+
let camelStr = "";
25+
for (let i = 0; i < str.length; i++) {
26+
// const element = array[i];
27+
// str = str[i].toUpperCase();
28+
// str = str.replaceAt(i, str[i].toUpperCase());
29+
// camelStr[i] = camelStr[i].toUpperCase();
30+
// console.log(str[i])
31+
32+
camelStr += i % 2 === 0 ? str[i].toUpperCase() : str[i];
33+
}
34+
// console.log([...str].map((st, index) => index % 2 ? st.toUpperCase() : st).join(""))
35+
36+
// console.log(camelStr);
37+
// return camelStr.join("");
38+
return camelStr;
39+
};
40+
41+
console.log(camelLetters("hello"));
42+
// camelLetters("yo eli")
43+
// camelLetters("hello????")

Interview-Questions/closeDiv.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Write a function that accepts a String as an argument.
3+
4+
The String is supposed to be HTML, but all the div elements are missing their closing tags (they have the < and >)
5+
6+
The function's job is to find and close all the divs in the provided HTML String
7+
8+
The function should return the entire corrected String.
9+
*/
10+
11+
// Edge cases
12+
/**
13+
* "<div><p>Here is a tag</p><div>" => "<div><p>Here is a tag</p></div>"
14+
*
15+
* "<div><div><p>Hello World</p><div><div>" => "<div></div><p>Here is a tag</p><div></div>"
16+
*
17+
* Second div should be closed div
18+
*/
19+
20+
const closeSecondDivs = (str) => {
21+
let divCounter = 0;
22+
let unknownFour = "";
23+
let fixedHTML = "";
24+
25+
for (let i = 0; i < str.length; i++) {
26+
if (str[i] === "<") {
27+
for (let j = 1; j < 5; j++) {
28+
unknownFour += str[i + j];
29+
}
30+
}
31+
32+
if (unknownFour === "div>") {
33+
divCounter++;
34+
if (divCounter % 2 === 0) {
35+
fixedHTML += str[i] + "/";
36+
unknownFour = "";
37+
continue;
38+
}
39+
}
40+
41+
fixedHTML += str[i];
42+
unknownFour = "";
43+
}
44+
45+
return fixedHTML;
46+
};
47+
48+
console.log(closeSecondDivs("<div><p>Here is a tag</p><div>"));
49+
console.log(closeSecondDivs("<div><div><p>Hello World</p><div><div>"));

0 commit comments

Comments
 (0)