CoDEVIANT #8 (3/29/19)

Not a great plan.
I’d like a car-wash and a bag of met-I mean…Skittles
function presses(phrase) {// To do…}
  • Create a variable starting at 0 to be our answer.
let answer = 0;
  • Create an object with
  • one being the key for values (in an array) ‘A’,’D’,’G’,’J’,’M’,’P’,’T’,’W’,’*’,’ ‘,’#’, ‘1’
  • two being the key for values in an array ‘B’,’E’,’H’,’K’,’N’,’Q’,’U’,’X’,’0'
  • three being the key for values in an array ‘C’,’F’,’I’,’L’,’O’,’R’,’V’,’Y’
  • four being the key for values in an array ‘S’,’Z’,’2',’3',’4',’5',’6',’8'
  • five being the key for values in an array ‘7’, ‘9’
let object = {one: [‘A’,’D’,’G’,’J’,’M’,’P’,’T’,’W’,’*’,’ ‘,’#’, ‘1’],two: [‘B’,’E’,’H’,’K’,’N’,’Q’,’U’,’X’,’0'],three: [‘C’,’F’,’I’,’L’,’O’,’R’,’V’,’Y’],four: [‘S’,’Z’,’2',’3',’4',’5',’6',’8'],five: [‘7’,’9']};
  • Now we turn the phrase argument (a string) we are passed into a version of itself being made entirely uppercase via the string method .toUpperCase( ) and then we turn THAT into an array with .split(‘’)
phrase = phrase.toUpperCase().split(‘’);
  • Now, since phrase is now an array of upperCased values, we use the .forEach( ) method on it with callback function arguments of current (for the current value) and index (for the index of the current value in the array phrase)
phrase.forEach((curr, index)=>{//good shit coming soon})
  • Then I originally had a series of for-loops where we would iterate over the length of the various arrays in our object that we created.
  • The gist is that if curr happens to equal any of the values in a given array, our answer variable would augment by a given integer. I wound up making 5 of them, in direct violation of D.R.Y. principles
for(let i = 0; i <= object.one.length — 1; i++){   if(curr == object.one[i]){   console.log(‘match’);   answer += 1;   console.log(answer);   }}for(let i = 0; i <= object.two.length — 1; i++){   if(curr == object.two[i]){   console.log(‘match 2’);   answer += 2;   console.log(answer);   }}for(let i = 0; i <= object.three.length — 1; i++){   if(curr == object.three[i]){   console.log(‘match 3’);   answer += 3;   console.log(answer);   }}for(let i = 0; i <= object.four.length — 1; i++){   if(curr == object.four[i]){   console.log(‘match 4’);   answer += 4;   console.log(answer);   }}for(let i = 0; i <= object.five.length — 1; i++){   if(curr == object.five[i]){   console.log(‘match 5’);   answer += 5;   console.log(answer);   }}
  • So I decided to make a function that would basically do all of that and avoid such a heavy repetition of code.
function doubleTime(array, number, curr) {   for(let c = 0; c <= array.length; c++){      if(curr == array[c]){         answer += number;      }   }}
function presses(phrase) {let answer = 0;let object = {   one: [‘A’,’D’,’G’,’J’,’M’,’P’,’T’,’W’,’*’,’ ‘,’#’, ‘1’],   two: [‘B’,’E’,’H’,’K’,’N’,’Q’,’U’,’X’,’0'],   three: [‘C’,’F’,’I’,’L’,’O’,’R’,’V’,’Y’],   four: [‘S’,’Z’,’2',’3',’4',’5',’6',’8'],   five: [‘7’,’9']};function doubleTime(array, number, curr) {   for(let c = 0; c <= array.length; c++){      if(curr == array[c]){         answer += number;      }   }}   phrase.forEach((curr, index)=>{      console.log(curr);      doubleTime(object.one,1, curr);      doubleTime(object.two,2,curr);      doubleTime(object.three, 3, curr);      doubleTime(object.four, 4, curr);      doubleTime(object.five, 5, curr);   });return answer;}
function presses(phrase) {var chunks = [‘1’, ‘ABC2’, ‘DEF3’, ‘GHI4’, ‘JKL5’, ‘MNO6’, ‘PQRS7’, ‘TUV8’, ‘WXYZ9’, ‘ 0’],phrase = phrase.toUpperCase().split(‘’),total = 0;   phrase.forEach(function(l) {      var key = chunks.filter(function(c) {      return c.indexOf(l) > -1;      })[0];      total += key.indexOf(l) + 1;    });   return total;}
  • They made three variables
  • chunks an array with groupings of the values based on how they are on the phone
  • phrase, just as we did, gets made into an uppercase version and then turned into an array.
  • total — zero.
  • They use a .forEach( ) method on the phrase array
[0]

Once upon a time, on a way through the old wild west,…

How I crossed the desert the smart way.

Other examples:

Task

Examples

See more examples in “Example Tests”

Note

function dirReduc(arr){let answer = [];function reducer(arr, answer){console.log(‘reducer running’);console.log(arr);for(let i = 0; i <= arr.length -1; i++){   if(arr[i] == “NORTH” && arr[i+1] ==”SOUTH”){   arr[i] = ‘’;   arr[i +1] = ‘’;   }if(arr[i] ==”EAST” && arr[i+1] ==”WEST”){   arr[i] = ‘’;   arr[i +1] = ‘’;   }}for(let z = 0; z <= arr.length-1; z++){   if(arr[z] == “SOUTH” && arr[z+1] == “NORTH”){      arr[z] = ‘’;      arr[z+1] = ‘’;   }   if(arr[z] ==”WEST” && arr[z+1] ==”EAST”){      arr[z] = ‘’;      arr[z +1] = ‘’;   }}arr = arr.filter(element => element.length > 1);console.log(‘filtered’);console.log(arr);   for(let x = 0; x <= arr.length-1; x++){   if(arr[x] == ‘’){   reducer(arr);   } else {   answer = arr;  return answer;   }  } }}
…kinda like this movie.
function dirReduc(plan) {var opposite = { ‘NORTH’: ‘SOUTH’, ‘EAST’: ‘WEST’, ‘SOUTH’: ‘NORTH’, ‘WEST’: ‘EAST’};return plan.reduce(function(dirs, dir){console.log(dirs);console.log(dir);console.log(‘jump’);   if (dirs[dirs.length — 1] === opposite[dir]) dirs.pop();     else dirs.push(dir); return dirs;   }, []);}
  • I added the console.logs so we could investigate
  • We make an object called opposite with keys and values being the opposites of eachother in terms of cardinal directions.
  • We return plan, our argument for the problem which is an array of a bunch of directions.
  • We use .reduce( ) and pass in dirs (which has a symbiotic relationship with the empty array at the end) as the accumulator or the reflection of the new array we are making with .reduce( ) then we pass in dir as the current value being dealt with in our callback function that is endemic to the array-method .reduce( ).
  • We then use an if-statement and say:
  • If dirs **initially an empty array** has a value at the index of it’s current length -1 that equals the value in the object opposite using next currentValue (aka dir) as a key…then it removes the last element of the array.
  • The value of opposite[dir] never even gets put in and then we go onto the next part of the plan array.
  • Yeah, that was pretty baller.
…I couldn’t resist.

Examples

function order(words){let array = [];words = words.split(‘ ‘);for(let i = 0; i <= words.length; i ++){   for(let c = 0; c <= words.length -1 ; c++){   let target = i + 1;   let block = words[c];      if(block.includes(target)) {         array.push(words[c]);      }   }}return array.join(‘ ‘);}
  • I made an empty array named…array (so creative).
  • I make our function’s argument words into an array.
  • We use two for-loops:
  • The first one iterates over the length of our new array words.
  • And for each single value in words, we will call it value-prime, with a counter of i
  • We have yet another for-loop that iterates over the length of words again and has a counter as c
  • We create a variable called target equalling i plus 1.
  • We create a variable called block equalling whatever word the nested for-loop is focusing on
  • Then we make an if-statement, where if the block variable we made includes target(which will always be a number)….then we will push whatever value is held by words[c].
  • We return the array as a string via the use of .join( )
function order(words){return words.split(‘ ‘).sort(function(a, b){return a.match(/\d/) — b.match(/\d/);}).join(‘ ‘);}
  • We turn our argument words into an array, and also make the elements of the array be picked based on whether they have an empty space between them.
  • Then on our array version of words we use an array method .sort( ).
  • if the returns a negative value, the value in a will be ordered before b. (ASCENDING)
  • if it returns 0, the ordering of a and b won’t change. (NOTHING)
  • if it returns a positive value, the value in b will be ordered before a. (DESCENDING)
…so practical ;)

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Intelligent, realtime and scalable video processing in Azure

Engineer to Engineer: How Open Source Can Welcome People into Tech

Reorganizing for success

The ever-changing landscape of modern applications

Identifying Hosts Class and Detecting Anomalies in Hosts Activity by Passive Network Traffic…

Devlog 18

7 Productive VSCode extensions 2021

5 things I wish I knew before adding variable in .env

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Rosales

Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

More from Medium

Control Statements in C

Introduction to Programming

Object Oriented Programming System(OOPs)

All About IDEs