Easy Coderbyte challenges 3 & 4
Aug. 11th, 2015 01:29 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Remembering these from last week: Challenge 3 was another I'd done before, "find the longest word in the sentence", and I got a few more refinements in my answer: I used a regular expression to create the first array; and (having discussed it with Ben) I used an empty string as the initializing value. The previous stab at this Reduce, where I was trying to initialize with 0, was comparing apples and oranges in the form of numbers and strings.
Challenge 4 was ...a journey. I discovered along the way that the ticking clock on these challenges is a joke. My first answer failed their tests (they do, at least, list the tests once you submit) and they then give you a chance to try again with a new clock, and this happens as many times as you like.
The challenge was, take a string of lowercase letters and random punctuation, shift all the letters in the string by one (b to c, y to z, z to a) and in the new coded string capitalize only vowels but make sure all the punctuation comes through unchanged. I had a ton of different errors, which was acutally kind of fun once I wasn't racing a clock. A few that I remember are that for awhile, down in the if statement that capitalizes vowels, I had if (lowercase == 'i' || 'a' || 'e') and that was coming up as always true. Everything was capitalized, whether there were vowels in the string or not. Stack Overflow helped point out what's wrong with the syntax there... As TimSmith-Aardwolf says, the if statement is asking if lowercase == i, or if a == a, or if e == e.
There was another error where a's in the input string weren't getting shifted: that was because in the if statement where I'm checking alfpos to make sure that I pass unfound things (characters not in my alf variable) through unchanged (their index comes back at -1), I had it as index position > 0 as the trigger to mess with that specifica character, instead of the working >= 0.
Here's my solution, which has a fair number of variables; but it doesn't seem much worse than anyone else's solution. I like the two alphabets instead of doing some special case for z, because I could with clarity adapt it to different offshifts. I could make my own ROT13 cipering tool. :)
function LongestWord(sen) { var arr = sen.split(/(\w+)/g); var foo = arr.reduce(function(prev, curr){ if(prev.length < curr.length){ return curr; } else {return prev;} }, ''); return foo; }Looking over other answers, I think reduce is by far the most elegant path. Wooo! Arrays FTW!
Challenge 4 was ...a journey. I discovered along the way that the ticking clock on these challenges is a joke. My first answer failed their tests (they do, at least, list the tests once you submit) and they then give you a chance to try again with a new clock, and this happens as many times as you like.
The challenge was, take a string of lowercase letters and random punctuation, shift all the letters in the string by one (b to c, y to z, z to a) and in the new coded string capitalize only vowels but make sure all the punctuation comes through unchanged. I had a ton of different errors, which was acutally kind of fun once I wasn't racing a clock. A few that I remember are that for awhile, down in the if statement that capitalizes vowels, I had if (lowercase == 'i' || 'a' || 'e') and that was coming up as always true. Everything was capitalized, whether there were vowels in the string or not. Stack Overflow helped point out what's wrong with the syntax there... As TimSmith-Aardwolf says, the if statement is asking if lowercase == i, or if a == a, or if e == e.
There was another error where a's in the input string weren't getting shifted: that was because in the if statement where I'm checking alfpos to make sure that I pass unfound things (characters not in my alf variable) through unchanged (their index comes back at -1), I had it as index position > 0 as the trigger to mess with that specifica character, instead of the working >= 0.
Here's my solution, which has a fair number of variables; but it doesn't seem much worse than anyone else's solution. I like the two alphabets instead of doing some special case for z, because I could with clarity adapt it to different offshifts. I could make my own ROT13 cipering tool. :)
function LetterChanges(str) { var alf = 'abcdefghijklmnopqrstuvwxyz'; var alf1 = 'bcdefghijklmnopqrstuvwxyza'; var foo = str.split(''); var bar = []; var baz = []; foo.forEach(function(letter){ var alfPos = alf.indexOf(letter); console.log(alfPos); if (alfPos >= 0) { bar.push(alf1[alfPos]); } else { bar.push(letter); } }); bar.forEach(function(lowercase){ if (lowercase == 'i' || lowercase == 'a' || lowercase == 'e' || lowercase == 'o' || lowercase == 'u') { baz.push(lowercase.toUpperCase()); } else { baz.push(lowercase); } }); return baz.join(''); } console.log(LetterChanges('ace!')); }