Skip to content

Instantly share code, notes, and snippets.

View LeanSeverino1022's full-sized avatar

Leandro Severino LeanSeverino1022

View GitHub Profile
@LeanSeverino1022
LeanSeverino1022 / random.js
Last active September 10, 2019 08:16
Javascript get random number in a specific range #vanillajs
//NOTE: Math.random() returns a floating point number from 0 up to but not including 1.
//1. inclusive at the minimum, exclusive at the maximum
var max = 10;
var min = 1;
var result = Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
/** helper function version **/
function getRandomInt(min, max) {
min = Math.ceil(min);
@LeanSeverino1022
LeanSeverino1022 / index.html
Last active September 2, 2019 06:25
[Serialize form data with jquery] from JS Bin// source http://jsbin.com/zofepa
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form action="">
<label for="name">Name:</label>
@LeanSeverino1022
LeanSeverino1022 / sum.js
Last active September 2, 2019 07:15
Compute Sum #vanillajs
//if you want the old way:
function sum(){
var sum =0;
for(var i=0;i<arguments.length;i++){
sum += arguments[i];
}
return sum;
}
sum(1,2); // returns 3
@LeanSeverino1022
LeanSeverino1022 / getLargestNum.js
Last active September 2, 2019 07:10
Find largest number #vanillajs
var numbers = [4, 44, 21, 29, 12, 7];
// Method 1: using Math.max
console.log( Math.max(...numbers) );
//Method 2: using reduce
var max = numbers.reduce(
function(previousLargestNumber, currentLargestNumber) {
return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;
}
@LeanSeverino1022
LeanSeverino1022 / README-Template.md
Last active February 26, 2023 05:42 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@LeanSeverino1022
LeanSeverino1022 / centerHScrollBar.js
Last active February 26, 2019 09:10
Position horizontal scroll bar at center of DIV (jQuery (2.1.1))
/*position horizontal scroll bar at center of DIV*/
// useful when for example the min-width of a scrollable container is wider than the viewport. Like in the t-shirt demo proj, I wanted the user to see the center of the shirt on load, rather than have the user need to scroll
function centerHScrollBar(){
var outerContent = $('.product-view');
var innerContent = $('.pillow-contain-show');
outerContent.scrollLeft( (innerContent.width() - outerContent.width()) / 2);
}
@LeanSeverino1022
LeanSeverino1022 / detectTouch.js
Last active September 2, 2019 09:05
detect if user is using touch device #vanillajs
//the FAV part is the approach I'd like to use. I've researched a lot about this and this seems to be a great choice. see https://codeburst.io/the-only-way-to-detect-touch-with-javascript-7791a3346685
// We really just want to know if the user is using their fingers to interact with our site.
// #1 basic logic
window.addEventListener('touchstart', function() {
//the user touched the screen
});
@LeanSeverino1022
LeanSeverino1022 / script.js
Last active September 2, 2019 06:32
capitalize first letter #strings #vanillajs
const capitalizeFirstLetter = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
capitalizeFirstLetter('puyih') //'Puyih'
capitalizeFirstLetter('p') //'P'
capitalizeFirstLetter(0) //''
capitalizeFirstLetter({}) //''
@LeanSeverino1022
LeanSeverino1022 / typecheck-1.js
Last active January 11, 2020 16:41
type checking techniques #types #vanillajs
//src: https://vanillajstoolkit.com/helpers/truetypeof/
/*!
* More accurately check the type of a JavaScript object
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Object} obj The object
* @return {String} The object type
*/
var trueTypeOf = function (obj) {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
@LeanSeverino1022
LeanSeverino1022 / index.html
Last active September 2, 2019 05:41
[Basic Library] usage inspired by jQuery - weird parts #designPatterns
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Library</title>
</head>
<body>
<h1 id="test">[greeting]</h1>
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>