MILESTONE 1: Complete Arcade Intro CodeSignal
Last week was really crazy with me. But everything is okay now. And here we go.
DONE: 15/60
Day04: I stuck on challenge 4 (ReverseInParentheses). And then I cannot stuck on it anymore. I decide to check the answer.
Here is what I learn:
Challenge 10: reverseInParentheses
Here is highest point solution in JS:
function reverseInParentheses(s) {
while (true) {
const closedIndex = s.indexOf(")");
if (closedIndex === -1) {
break;
}
const openIndex = s.substring(0, closedIndex).lastIndexOf("(");
const start = s.substring(0, openIndex);
const middle = s
.substring(openIndex + 1, closedIndex)
.split("")
.reverse("")
.join("");
const end = s.substring(closedIndex + 1, s.length);
s = start + middle + end;
}
return s;
}
What I learn here:
- I learn the logic to find out which string should be reverse first.
Challenge 11: alternatingSums
Here is my solution:
function alternatingSums(a) {
return a.reduce((acc, curr, index) => {
if(index % 2 == 0) {
acc[0] += curr
} else acc[1] += curr;
return acc;
}, [0, 0])
}
I findout the way to use reduce for this problem.
Here is highest point solution in JS:
alternatingSums = a => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
What I learn here:
- I learn about & operator in JS. But this is kinda complex to read and understand.
Challenge 12: addBorder
Here is my solution:
function addBorder(picture) {
const initalElementLength = picture[0].length;
picture = picture.map((el) => {
return `*${el}*`;
});
let borderTopDown = "**";
for (let i = 0; i < initalElementLength; i++) {
borderTopDown += "*";
}
picture.push(borderTopDown);
picture.unshift(borderTopDown);
return picture;
}
This challenge kinda simple.
Here is highest point solution in JS:
function addBorder(picture) {
var width = picture[0].length + 2;
return [
"*".repeat(width),
...picture.map((line) => `*${line}*`),
"*".repeat(width),
];
}
What I learn here:
- Try to loop only one when use this.
- The repeat() method of string.
Okay. I done for today. I hope tomorrow will be better. Let's go.
#inanotherlife