Skip to content

Instantly share code, notes, and snippets.

@Shavindra
Shavindra / sortTemps.js
Created April 24, 2020 22:53
Value closest to zero
const sortedTemps = temps.sort((a, b) => Math.abs(a) - Math.abs(b) || b - a);
const result = sortedTemps[0] || 0;
console.log(result);
@Shavindra
Shavindra / useful-methods.js
Created November 10, 2019 22:27
useful methods
// 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"
@Shavindra
Shavindra / FlattenArray.js
Created October 17, 2019 19:51
Flatten Array
const flattenArray = (arr) => {
return arr.reduce((a, v) => {
return a.concat(Array.isArray(v) ? flattenArray(v) : v)
}, []);
}
// ESLint.com
// https://eslint.org/demo#eyJ0ZXh0IjoiY29uc3QgZmxhdHRlbkFycmF5ID0gKGFycikgPT4ge1xuICByZXR1cm4gYXJyLnJlZHVjZSgoYWNjdSwgY3VycikgPT4ge1xuICAgIHJldHVybiBhLmNvbmNhdChBcnJheS5pc0FycmF5KGN1cnIpID8gZmxhdHRlbkFycmF5KGN1cnIpIDogY3VycilcbiAgfSwgW10pO1xufSIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6Nywic291cmNlVHlwZSI6InNjcmlwdCIsImVjbWFGZWF0dXJlcyI6e319LCJydWxlcyI6e30sImVudiI6eyJicm93c2VyIjp0cnVlLCJub2RlIjp0cnVlLCJjb21tb25qcyI6dHJ1ZSwic2hhcmVkLW5vZGUtYnJvd3NlciI6dHJ1ZSwid29ya2VyIjp0cnVlLCJhbWQiOnRydWUsIm1vY2hhIjp0cnVlLCJqYXNtaW5lIjp0cnVlLCJqZXN0Ijp0cnVlLCJwaGFudG9tanMiOnRydWUsImpxdWVyeSI6dHJ1ZSwicXVuaXQiOnRydWUsInByb3RvdHlwZWpzIjp0cnVlLCJzaGVsbGpzIjp0cnVlLCJtZXRlb3IiOnRydWUsIm1vbmdvIjp0cnVlLCJwcm90cmFjdG9yIjp0cnVlLCJhcHBsZXNjcmlwdCI6dHJ1ZSwibmFzaG9ybiI6dHJ1ZSwic2VydmljZXdvcmtlciI6dHJ1ZSwiYXRvbXRlc3QiOnRydWUsImVtYmVydGVzdCI6dHJ1ZSwid2ViZXh0ZW5zaW9ucyI6dHJ1ZS
//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 September 24, 2015 10:50
Fundamentals: get all the arguments in a function
//Function declaration
function foo() {
console.log(arguments);
}
foo(['a', 'b', 'c'], [1, 2, 3, 4, 5]);
//IIFE
(function() {
@Shavindra
Shavindra / gistMarkdown.js
Last active August 29, 2015 14:27
Dynamically embed Gists in a markdown document
/* 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>
*
*
*/
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);
}
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'
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
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