madbernard: a long angled pier (Default)
[personal profile] madbernard
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.
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 = [];
    var alfPos = alf.indexOf(letter);
    if (alfPos >= 0) {
    } else {
    if (lowercase == 'i' || lowercase == 'a' || lowercase == 'e' || lowercase == 'o' || lowercase == 'u') {
    } else {
  return baz.join('');

Identity URL: 
Account name:
If you don't have an account you can create one now.
HTML doesn't work in the subject.


Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

May 2016

8910 11121314

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 22nd, 2017 06:40 pm
Powered by Dreamwidth Studios