Skip to content

Instantly share code, notes, and snippets.

@Shavindra
Shavindra / sortTemps.js
Created Apr 24, 2020
Value closest to zero
View sortTemps.js
const sortedTemps = temps.sort((a, b) => Math.abs(a) - Math.abs(b) || b - a);
const result = sortedTemps[0] || 0;
console.log(result);
View useful-methods.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
console.log('10'.padStart(2, '0')); // expected output: "10"
console.log('9'.padStart(2, '0')); // expected output: "09"
View FlattenArray.js
const flattenArray = (arr) => {
return arr.reduce((a, v) => {
return a.concat(Array.isArray(v) ? flattenArray(v) : v)
}, []);
}
// ESLint.com
// https://eslint.org/demo#eyJ0ZXh0IjoiY29uc3QgZmxhdHRlbkFycmF5ID0gKGFycikgPT4ge1xuICByZXR1cm4gYXJyLnJlZHVjZSgoYWNjdSwgY3VycikgPT4ge1xuICAgIHJldHVybiBhLmNvbmNhdChBcnJheS5pc0FycmF5KGN1cnIpID8gZmxhdHRlbkFycmF5KGN1cnIpIDogY3VycilcbiAgfSwgW10pO1xufSIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6Nywic291cmNlVHlwZSI6InNjcmlwdCIsImVjbWFGZWF0dXJlcyI6e319LCJydWxlcyI6e30sImVudiI6eyJicm93c2VyIjp0cnVlLCJub2RlIjp0cnVlLCJjb21tb25qcyI6dHJ1ZSwic2hhcmVkLW5vZGUtYnJvd3NlciI6dHJ1ZSwid29ya2VyIjp0cnVlLCJhbWQiOnRydWUsIm1vY2hhIjp0cnVlLCJqYXNtaW5lIjp0cnVlLCJqZXN0Ijp0cnVlLCJwaGFudG9tanMiOnRydWUsImpxdWVyeSI6dHJ1ZSwicXVuaXQiOnRydWUsInByb3RvdHlwZWpzIjp0cnVlLCJzaGVsbGpzIjp0cnVlLCJtZXRlb3IiOnRydWUsIm1vbmdvIjp0cnVlLCJwcm90cmFjdG9yIjp0cnVlLCJhcHBsZXNjcmlwdCI6dHJ1ZSwibmFzaG9ybiI6dHJ1ZSwic2VydmljZXdvcmtlciI6dHJ1ZSwiYXRvbXRlc3QiOnRydWUsImVtYmVydGVzdCI6dHJ1ZSwid2ViZXh0ZW5zaW9ucyI6dHJ1ZS
View capita-utils.js
//uitls.js : CREATE NAMESPACING functions also use ‘define()’ to declare it as requirjs module
(function (requirejs) {
define('utils', function () {
// Allow creation of methods for SHOP modules
window.UTILS = window.UTILS || {};
var u = window.UTILS;
@Shavindra
Shavindra / Fundamentals - anyNumberOfArguments.js
Last active Sep 24, 2015
Fundamentals: get all the arguments in a function
View Fundamentals - anyNumberOfArguments.js
//Function declaration
function foo() {
console.log(arguments);
}
foo(['a', 'b', 'c'], [1, 2, 3, 4, 5]);
//IIFE
(function() {
@Shavindra
Shavindra / gistMarkdown.js
Last active Aug 29, 2015
Dynamically embed Gists in a markdown document
View gistMarkdown.js
/* USAGE:
* markdown, gist, github, json, embed
* Reference the script in index.html (or main md html file e.g. mdwiki.html)
* Then in any .md document include like below. Ensure empty lines before <div> and after </div>
*
* <div class="gistEmbed" data-gist-id="12265cd979f0dee75acd"></div>
*
*
*/
View Java examples: Enhanced For Loop Statement.java
public static void enhancedForLoop(){
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("\nEXAMPLE: Enhanced For Statement");
for (int temp: arr){ // Create a temporary variable, same type as array values. For
// loop assign array value to the temporary variable
System.out.println("Enhanced = " + temp);
}
View Java examples: Switch Statement.java
public static void witchStatment(int num) {
System.out.println("\nEXAMPLE: Switch Statement");
switch(num){ //Check the value
case 0:
System.out.println("Case = " + num); // Execute this if 'case' matches the 'value'
break; // Stop the statement
case 1:
System.out.println("Case = " + num); // Execute this if 'case' matches the 'value'
View Java examples: 'do'-while loop.java
public static void doWhileLoop() {
System.out.println("\nEXAMPLE: Do While Loop");
int num = 0;
do {
// DO the work... Execute the loop first
System.out.println("num = " + num);
num++; //increment 'l' by 1
View Java examples: WhileLoop.java
public static void whileLoop(){
System.out.println("\nEXAMPLE: While Loop");
int num = 0;
while(num <= 20) { // Check the while condition first, Then execute the loop
// Then while 'l' is less than or equal to '0'
System.out.println("num = " + num);
num++; //increment 'l' by 1