3h Today was a fun balance of react work during the day and js coding challenges at night. I did them on coderbyte again, and it was fun again. I did 5 challenges tonight. I like having to think fast sometimes, as it forces me to work on my speed debugging skills. I ran into some things tonight that I actually wanted to refactor after I had submitted and that process was pretty useful as well.
Count number of words in a string
I felt pretty good about this being my first problem of the night. They only get harder from here though!
function WordCount(str) { return str.split(" ").length; }
WordCount(readline());
Count up x’s and o’s in a string and return if they are equal in number
I could do the for loop with a nested ternary, but I think this is pretty readable. I just remembered that I had a lot of trouble with this one not working at first, and that was because I misread it and had ‘x’ and ‘y’! Sheesh. I need to slow down for the first minute to make sure I’m going the right direction.
function ExOh(str) {
var xCount = 0
var oCount = 0
for (i=0;i<str.length;i++){
if (str[i]==="o"){ oCount += 1 }
if (str[i]==="x"){ xCount += 1 }
}
return (xCount == oCount);
}
ExOh(readline());
palindrome checker
I thought this was a simple solution.
function Palindrome(str) {
str=str.split(" ").join("").toLowerCase() //remove whitespace
for (let i = 0; i < str.length; i++){
if (str[i] != str[str.length-i-1]){
return false
}
}
return true;
}
Palindrome(readline());
Longest increasing subsequence
For this one I had some questions. I didn’t pass all of the tests, and the rules are pretty unclear. It seems like the two tests I failed required me to skip a number mid-sequence and then continue on. That is definitely a way to do it, but that also seems like a different problem.
function LongestIncreasingSequence(arr) {
let counter = 1
let maxCounter = 0
let currentNumber = 0
myArr = []
for (let i = 0; i < arr.length-1; i++){
currentNumber = arr[i]
myArr.push(arr[i])
for (let j = i; j < arr.length-1; j++){
if (currentNumber < arr[j+1]) {
currentNumber = arr[j+1]
counter ++
myArr.push(arr[j+1])
}
}
if (counter > maxCounter){
maxCounter = counter
}
counter = 1
console.log(myArr)
myArr=[]
}
return maxCounter;
}
LongestIncreasingSequence(readline());
Return the first non-repeated character in a string
this one was fun. I had no idea when I first started it how to do it, then the idea of making a count function and passing chars into it until I hit a ‘1’ count started forming. One thought after doing three of these tonight is that I should probably be using the map function. I’m going to refactor it after this. It might save some typing.
function NonrepeatingCharacter(str) {
for (let i = 0; i<str.length; i++){
if (getCharCount(str[i], str) == 1){
return str[i]
}
}
return str;
}
function getCharCount(letter, str){
count = 0
for (let i = 0; i<str.length; i++){
if (str[i] == letter){ count ++ }
}
return count
}
NonrepeatingCharacter(readline());
Here is the getCharCount function refactored using map. it’s the same damn length! I could probably make it a 2 liner by moving the return up to the str.split line and then making that whole blob unformatted. I don’t think I’ll do that because it would be long and unreadable though. I did have another thought about how I can probably do this with regex capture groups. I’ll try that next.
function getCharCount(letter, str){
count = 0
str.split("").map((char, index) => {
if (char == letter){ count ++ }
})
return count
}
Oof! I burned an hour looking into regex lookaheads and backreferences. I don’t think I will get this one tonight, but it was still good to look into it. It was a good session. Can’t wait for more coding tomorrow.