Skip to content

Instantly share code, notes, and snippets.

@ericlagergren
Created June 25, 2014 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericlagergren/1a61b5d772ae49ab3aea to your computer and use it in GitHub Desktop.
Save ericlagergren/1a61b5d772ae49ab3aea to your computer and use it in GitHub Desktop.
explaining my js
// Set the variables R, C, and U equal to 'replace', 'charAt', and 'toUpperCase'
// Replace searches a string and replaces characters
// charAt finds the char at a certain position
// toUpperCase makes things uppercase
R="replace",
C="charAt",
U="toUpperCase";
// alert causes an on-screen pop-up, needed for this competition because I have to write to STDOUT
alert(
// a[R] means the same as a.replace() see variable `R` above
a[R](
// This regexp looks for any of those words (COMMA, SEMICOLON, etc) that match the pattern "^WORD"
/\^((COMMA)|(SEMICOLON)|(COLON)|(PERIOD)|(BANG)|(DASH)|(HYPHEN)|(EMDASH)|(OPENQUOTE)|(CLOSEQUOTE))/g
// Because I'm using replace() I can declare the replacement as a function
// In order to do so, I'm using ECMA 6's arrow functions
// Arrow functions follow the syntax of:
/*
([param] [, param]) => {
statements
}
param => expression
*/
// So, I'm essentially saying, a.replace([regexp], function(args) {if (a) { do things } else if { do more things} })
,((m,_,a,b,c,d,e,f,g,h,i,j)=>a?",":b?";":c?":":d?".":e?"!":f?"-":g?"-h-":h?"-e-":i?' "':'" '))
// This is the same thing, except I'm using different regexps
// These say, match any string that's something like "[space][punctuation]"
// I have to list all these in order because they're being mapped to my arguments
// So the (\.) is mapped to argument 'v', hence the v?"."
[R](/\s((\.)|(\!)|(\,)|(\;)|(\:)|(\-\h\-\s)|(\-\e\-\s))/g,((k,l,v,n,o,p,q,r,s)=>v?".":n?"!":o?",":p?";":q?":":r?"-":"--"))[R](/[^!,"'.]\"\s/g,'"')
// Again, same as above except this time I'm taking the [C]haracter at position (0) and making it [U]ppercase() then taking the rest of the string and tacking it on to the end of that character
[R](/.+?[\.\?\!](\s|$)/g,(t=>t[C](0)[U]()+t.substr(1)))
// Nearly the same... figure it out >:)
[R](/\"[a-z]/g,(u=>u[C](0)+u[C](1)[U]())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment