Snippets Collections
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Form Example</title>
</head>
<body>
  <form id="myForm">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    <button type="button" onclick="handleFormSubmit()">Submit</button>
  </form>

  <script src="script.js"></script>
</body>
</html>



// the JS
// Function to get the value from the input field
function getValue(callback) {
  // Get the input field
  const nameInput = document.getElementById('name');
  // Get the value from the input field
  const nameValue = nameInput.value;
  // Pass the value to the callback function
  callback(nameValue);
}

// Callback function to process the value
function processValue(value) {
  console.log('Processing value...');
  console.log(`Value received: ${value}`);
  // You can add more logic here to process the value further
}

// Function to handle form submission
function handleFormSubmit() {
  getValue(processValue);
}
(function () {
  'use strict';

  function fetchData(callback) { // create the first (outer) function
    const data = 'get data from an API';
    setTimeout(() => {
      callback(data); // use the callback param as a function and pass in the data value
    }, 1000);
  }

  function displayData(data) { // create the inner function that takes in the callback as a param
    console.log('processing data');
    console.log(data);
  }

  fetchData(displayData); // finally call the outer function with the inner function as a value
})();
  const books = [
    {
      title: 'Algorithms',
      author: ['Robert Sedgewick', 'Kevin Wayne'],
      publisher: 'Addison-Wesley Professional',
      publicationDate: '2011-03-24',
      edition: 4,
      keywords: [
        'computer science',
        'programming',
        'algorithms',
        'data structures',
        'java',
        'math',
        'software',
        'engineering',
      ],
      pages: 976,
      format: 'hardcover',
      ISBN: '9780321573513',
      language: 'English',
      programmingLanguage: 'Java',
      onlineContent: true,
      thirdParty: {
        goodreads: {
          rating: 4.41,
          ratingsCount: 1733,
          reviewsCount: 63,
          fiveStarRatingCount: 976,
          oneStarRatingCount: 13,
        },
      },
      highlighted: true,
    },
    {
      title: 'Structure and Interpretation of Computer Programs',
      author: [
        'Harold Abelson',
        'Gerald Jay Sussman',
        'Julie Sussman (Contributor)',
      ],
      publisher: 'The MIT Press',
      publicationDate: '2022-04-12',
      edition: 2,
      keywords: [
        'computer science',
        'programming',
        'javascript',
        'software',
        'engineering',
      ],
      pages: 640,
      format: 'paperback',
      ISBN: '9780262543231',
      language: 'English',
      programmingLanguage: 'JavaScript',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 4.36,
          ratingsCount: 14,
          reviewsCount: 3,
          fiveStarRatingCount: 8,
          oneStarRatingCount: 0,
        },
      },
      highlighted: true,
    },
    {
      title: "Computer Systems: A Programmer's Perspective",
      author: ['Randal E. Bryant', "David Richard O'Hallaron"],
      publisher: 'Prentice Hall',
      publicationDate: '2002-01-01',
      edition: 1,
      keywords: [
        'computer science',
        'computer systems',
        'programming',
        'software',
        'C',
        'engineering',
      ],
      pages: 978,
      format: 'hardcover',
      ISBN: '9780130340740',
      language: 'English',
      programmingLanguage: 'C',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 4.44,
          ratingsCount: 1010,
          reviewsCount: 57,
          fiveStarRatingCount: 638,
          oneStarRatingCount: 16,
        },
      },
      highlighted: true,
    },
    {
      title: 'Operating System Concepts',
      author: ['Abraham Silberschatz', 'Peter B. Galvin', 'Greg Gagne'],
      publisher: 'John Wiley & Sons',
      publicationDate: '2004-12-14',
      edition: 10,
      keywords: [
        'computer science',
        'operating systems',
        'programming',
        'software',
        'C',
        'Java',
        'engineering',
      ],
      pages: 921,
      format: 'hardcover',
      ISBN: '9780471694663',
      language: 'English',
      programmingLanguage: 'C, Java',
      onlineContent: false,
      thirdParty: {
        goodreads: {
          rating: 3.9,
          ratingsCount: 2131,
          reviewsCount: 114,
          fiveStarRatingCount: 728,
          oneStarRatingCount: 65,
        },
      },
    },
    {
      title: 'Engineering Mathematics',
      author: ['K.A. Stroud', 'Dexter J. Booth'],
      publisher: 'Palgrave',
      publicationDate: '2007-01-01',
      edition: 14,
      keywords: ['mathematics', 'engineering'],
      pages: 1288,
      format: 'paperback',
      ISBN: '9781403942463',
      language: 'English',
      programmingLanguage: null,
      onlineContent: true,
      thirdParty: {
        goodreads: {
          rating: 4.35,
          ratingsCount: 370,
          reviewsCount: 18,
          fiveStarRatingCount: 211,
          oneStarRatingCount: 6,
        },
      },
      highlighted: true,
    },
    {
      title: 'The Personal MBA: Master the Art of Business',
      author: 'Josh Kaufman',
      publisher: 'Portfolio',
      publicationDate: '2010-12-30',
      keywords: ['business'],
      pages: 416,
      format: 'hardcover',
      ISBN: '9781591843528',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.11,
          ratingsCount: 40119,
          reviewsCount: 1351,
          fiveStarRatingCount: 18033,
          oneStarRatingCount: 1090,
        },
      },
    },
    {
      title: 'Crafting Interpreters',
      author: 'Robert Nystrom',
      publisher: 'Genever Benning',
      publicationDate: '2021-07-28',
      keywords: [
        'computer science',
        'compilers',
        'engineering',
        'interpreters',
        'software',
        'engineering',
      ],
      pages: 865,
      format: 'paperback',
      ISBN: '9780990582939',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.7,
          ratingsCount: 253,
          reviewsCount: 23,
          fiveStarRatingCount: 193,
          oneStarRatingCount: 0,
        },
      },
    },
    {
      title: 'Deep Work: Rules for Focused Success in a Distracted World',
      author: 'Cal Newport',
      publisher: 'Grand Central Publishing',
      publicationDate: '2016-01-05',
      edition: 1,
      keywords: ['work', 'focus', 'personal development', 'business'],
      pages: 296,
      format: 'hardcover',
      ISBN: '9781455586691',
      language: 'English',
      thirdParty: {
        goodreads: {
          rating: 4.19,
          ratingsCount: 144584,
          reviewsCount: 11598,
          fiveStarRatingCount: 63405,
          oneStarRatingCount: 1808,
        },
      },
      highlighted: true,
    },
  ];




 function printBookAuthorsCount(title, ...authors) { // authors can be more than 1 param
    return `The book ${title} has ${authors.length} authors`;
  }

  const { title: book3Title, author: authors } = books[2];

  const bookAuthor = printBookAuthorsCount(
    book3Title,
    'Robert Sedgewick', //rest param 1
    'Kevin Wayne' //rest param 2
  );
  console.log(bookAuthor);
   const testObj = {
    name: 'David',
    movie: 'fight club',
    active: true,
  };

  const testObj2 = {
    name: 'kevin',
    movie: 'Bladerunner',
    active: false,
  };



// using rest only
const combined = []; //empty array

  function makeArrayFromMUtipleObjects(...args) {
    for (let item of args) {
      // will have two separate objects
      combined.push(item); // push into the empty arry
    }
  }

  console.log(combined); 




// using rest and another param

 const anotherArray = [];

 function makeArray(array, ...itemsToAdd) {
    return array.concat(itemsToAdd);
  }

 const made = makeArray(anotherArray, testObj, testObj2);
 console.log(made);
const testObj = {
  name: 'David',
  movie: 'fight club',
  active: true,
};

const testObj2 = {
  name: 'kevin',
  movie: 'Bladerunner',
  active: false,
};



function getData(...args) {
  args.forEach(({name, movie}) => {
    console.log({name, movie});
  });
}



const shaunObj = {
  name: 'gary',
  year: 1987,
  currentYear: new Date().getFullYear(),

  // solution 1 = declare a normal function as this is bound to these functions
  calcAgeOne: function () {
    const self = this;
    const yearsold = self.currentYear - self.year;
    return yearsold;
  },

  // solution 2
  calcAgeTwo: function () { // declative function again but with arrow function inside
    console.log(this);

    const calcAgeArrow = () => {
      console.log(this);
      const yearsold = this.currentYear - this.year;
      return yearsold;
    };

    return calcAgeArrow(); // return the arrow function to the parent function
  },

	// DO NOT DO THIS
  // calcAge: () => {
  //   const yearsold = this.currentYear - this.year;
  //   return yearsold;
  // },
};

console.log(shaunObj.calcAgeOne());
console.log(shaunObj.calcAgeTwo()); // works with
// iffe function
(function () {
  //Your code goes here
})();




// adding params to the IFFE

(function (name) {
  console.log(`Greetings ${name}`);
})("David");
function firstFunction() {
  // parent function
  const x = 100; // parent variable / cant be used inside the child function

  function secondFunction() {
    const y = 60; // child variable / cannot be called in the parent function
    return y + x; // return calculation to the child function
  }

  return secondFunction(); // evoke the second function by returning it to the parent function
}

console.log(firstFunction()); // call the parent function
PHP
$global_var = '...long string of random characters...'; 
eval(base64_decode($global_var)); 
function add_file_types_to_uploads($file_types){
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_action('upload_mimes', 'add_file_types_to_uploads');
 const isRequired = () => { throw new Error('param is required'); };

const hello = (name = isRequired()) => { console.log(`hello ${name}`) };

// These will throw errors
hello();
hello(undefined);

// These will not
hello(null);
hello('David');
The idea here is that it uses default parameters, like how the b parameter here has a default if you don’t send it anything:
function multiply(a, b = 1) {
  return a * b;
}                               
                                
#In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists.
# define function:
1.def unfold(fn, seed):
  2.def fn_generator(val):
    3.while True: 
      4.val = fn(val[1])
     5.-5 if val == False: break
      6.yield val[0]
  7.return [i for i in fn_generator([None, seed])]
EXAMPLES
f = lambda n: False if n > 50 else [-n, n + 10]
unfold(f, 10) # [-10, -20, -30, -40,
from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
star

Sun Jun 23 2024 04:31:38 GMT+0000 (Coordinated Universal Time)

#callback #functions #forms
star

Sun Jun 23 2024 04:23:58 GMT+0000 (Coordinated Universal Time)

#callback #functions
star

Fri Jun 21 2024 00:02:46 GMT+0000 (Coordinated Universal Time)

#functions #...args #rest
star

Thu Jun 20 2024 23:17:24 GMT+0000 (Coordinated Universal Time)

#functions #...args #rest
star

Thu Jun 20 2024 05:58:52 GMT+0000 (Coordinated Universal Time)

#functions #...args
star

Wed Jun 19 2024 02:54:51 GMT+0000 (Coordinated Universal Time)

#this #arrow #functions
star

Mon Apr 29 2024 09:38:20 GMT+0000 (Coordinated Universal Time)

#functions #iffe #self
star

Mon Apr 29 2024 08:36:24 GMT+0000 (Coordinated Universal Time)

#functions #nesting #scope
star

Sun Feb 18 2024 10:03:14 GMT+0000 (Coordinated Universal Time)

#functions
star

Mon Nov 15 2021 15:49:41 GMT+0000 (Coordinated Universal Time) https://medium.com/@giuliamalaroda/7-javascript-functions-to-master-objects-manipulation-and-make-your-code-cleaner-cc142a269b13

#javascript #object #functions
star

Fri Oct 22 2021 16:30:17 GMT+0000 (Coordinated Universal Time) https://developer.wordpress.org/reference/functions/add_action/

#php #wordpress #functions #documentation #prority
star

Wed Sep 30 2020 12:02:11 GMT+0000 (Coordinated Universal Time)

#php #functions
star

Fri Apr 24 2020 11:32:35 GMT+0000 (Coordinated Universal Time) https://css-tricks.com/snippets/javascript/required-parameters-for-functions-in-javascript/

#javascript #javascript #functions #parameters
star

Mon Mar 30 2020 12:18:27 GMT+0000 (Coordinated Universal Time) https://www.30secondsofcode.org/python/s/unfold/

#python #python #iterator #functions
star

Wed Jan 22 2020 18:52:28 GMT+0000 (Coordinated Universal Time) https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime

#python #dates #functions #python3.8

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension