Week 14 Sandbox

Functional JS: Declarative vs Imperative Using Array Prototype Functions

Matching Via RegExp Imperatively

        
          let names = ["John Williams", "Michael Pollan", "Carl Smith", "Shannon Wheeler", "Kiyohiko Azuma", "Al Burian", "John Smith"];

          function imperativeNameCheck(names, regexp){
            let passedNames = [];

            for(let i = 0; i < names.length; i++)
              if(names[i].match(regexp))
                passedNames.push(names[i]);

            return passedNames;
          }

          let johns = imperativeNameCheck(names, /(Smith)/)
          console.log(johns);      //[ 'Carl Smith', 'John Smith' ]
        
      

Matching Via RegExp Declaratively Using filter()

        
          const names = ["John Williams", "Michael Pollan", "Carl Smith", "Shannon Wheeler", "Kiyohiko Azuma", "Al Burian", "John Smith"];

          const decJohns= names.filter(someName => someName.match(/(Smith)/));

          console.log((decJohns));    //[ 'Carl Smith', 'John Smith' ]
        
      

Creating a Full Relative Filepath from a Group of Images Imperatively

        
          let partialPath = ["dog.jpg", "house.png", "motorcyle.gif", "ant.jpg", "river.jpeg"];

          function fullRelative(startPath, endArray){
            let fullPath = [];

            for(let i = 0; i < endArray.length; i++)
              fullPath.push(startPath + endArray[i]);

            return fullPath;
          }

          console.log(fullRelative("/home/cryptodwayne/images/", partialPath));    //[ '/home/cryptodwayne/images/dog.jpg', etc.
        
      

Creating a Full Relative Filepath from a Group of Images Declaratively Using map()

        
          const partialPath = ["dog.jpg", "house.png", "motorcyle.gif", "ant.jpg", "river.jpeg"];

          const decFullPath = partialPath.map(end => `/home/cryptodwayne/images/${end}`);

          console.log(decFullPath);    //[ '/home/cryptodwayne/images/dog.jpg', etc.
        
      

Telling if all Numbers in an Array Are Integers Imperatively

        
          let numbers = [1, 73.42, 7, 182.1244, 6, 29.12, 9];

          function isInteger(numArray){

            for(let i = 0; i < numArray.length; i++)
              if(numArray[i] % 1 != 0)
                return false;

            return true;
          }

          console.log(isInteger(numbers));    //false
        
      

Telling if a Number is an Intger Declaratively Using every()

        
          const numbers = [1, 73.42, 7, 182.1244, 6, 29.12, 9];

          const decIsInts = numbers.every(num => num % 1 == 0);

          console.log(decIsInts);    //false
        
      

Telling if Anyone in a Group of People is Under Age 21 Imperatively

        
          //Imperative
          let people = [{ name: "John Smith",
                          age: 21,
                          city: "Portland"
                        },
                        { name: "Marcus Williams",
                          age: 16,
                          city: "Portland"
                        },
                        { name: "Yuassa Masaki",
                          age: 43,
                          city: "Portland"
                        }
                       ];

          function under21(personArray){
            for(let i = 0; i < personArray.length; i++)
              if(personArray[i].age < 21)
                return true;

            return false;
          }

          console.log(under21(people));  //true
        
      

Telling if Anyone in a Group of People is Under Age 21 Declaratively Using some()

        
          const people = [{ name: "John Smith",
                          age: 21,
                          city: "Portland"
                        },
                        { name: "Marcus Williams",
                          age: 16,
                          city: "Portland"
                        },
                        { name: "Yuassa Masaki",
                          age: 43,
                          city: "Portland"
                        }
                       ];

            const decOver21 = people.some((person => person.age < 21));

            console.log(under21(people));    //true
        
      

Reversing an Array Imperatively

        
          let someNums = [0, 1, 2, 3, 4, 5, 6, 7];

          function reverseArray(array){
            let reversed = [];

            for(let i = array.length-1; i >= 0; i--)
              reversed.push(array[i]);

            return reversed;
          }

          console.log(reverseArray(someNums));    //[ 7, 6, 5, 4, 3, 2, 1, 0 ]
        
      

Reversing an Array Declaratively Using reverse()

        
          const someNums = [0, 1, 2, 3, 4, 5, 6, 7];

          const reversedArray = someNums.reverse();

          console.log(reversedArray);    //[ 7, 6, 5, 4, 3, 2, 1, 0 ]