Skip to content

Instantly share code, notes, and snippets.

View williamromero's full-sized avatar
🏠
Working from home

William Romero williamromero

🏠
Working from home
View GitHub Profile
@williamromero
williamromero / minimum_moves.rb
Last active May 6, 2022 17:56
Hackerank :: Minimum moves two arrays of integers
def minimum_moves(*arrays)
return raise 'Length of arrays are not equal' if arrays.first.length != arrays.last.length
moves = 0
slice_number = proc { |n| n.to_s.chars.map(&:to_i) }
arrays.length.times do |i|
first = slice_number.(arrays.first[i])
second = slice_number.(arrays.last[i])
first.map.with_index { |n, i| moves += (first[i] < second[i]) ? (second[i] - first[i]) : (first[i] - second[i]) unless (first[i] == second[i]) && (first.length != second.length) }
end
@williamromero
williamromero / sequence_length.rb
Last active May 5, 2022 18:39
This code checks if exists a sequence of numbers inside of an array and retrieves those numbers ordered.
def sequence_length(array)
array.sort { |a, b| a <=> b }.each_cons(2) { |a, b| b - a == 1 ? array : array.delete(b) }
"Length on sequence: #{array.length} - #{array.sort}"
end
p sequence_length([100, 4, 200, 1, 3, 2]) # Length on sequence: 4 - [1, 2, 3, 4]
p sequence_length([29, 27, 28, 55, 100, 84]) # Length on sequence: 3 - [27, 28, 29]
@williamromero
williamromero / filter-products.js
Last active May 24, 2020 04:50
Code Snippet to Filter by Brand
$('a[filter-by="m&m"]').click(function(){
var mix = $('.mix').children('.text-block-8');
mix.each(function(index){
if ($(this).text() === 'M&M') {
$(this).parent().show();
} else {
$(this).parent().hide();
}
});
});
window.addEventListener("load", function() {
alert('HOLA MUNDO');
});
body {
background-color: red;
display: block;
height: 100%;
margin: 0 auto;
width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Page Title</title>
</head>
<body>
<h1>Hello, world!</h1>
@williamromero
williamromero / js
Created August 21, 2019 05:36
Upload Base 64 Image to Active Storage
var ffachada = document.querySelector('.ffachada'); // InputFile
var idData = document.querySelector('idData).innerHTML; // Crgando ID record del formulario
var formElm = document.querySelector('.edit_visitors').getAttribute('action');
var imgBSC;
ffachada.addEventListener('change', (e) => {
e.preventDefault();
var file = e.target.files[0]; // Tomamos la imagen del InputFile
var reader = new FileReader(); // Creamos un objeto FileReader
reader.readAsDataURL(file); // Codificador a Base64

leads_controller.rb

    def activate
      # @leads = Lead.all.where("type = ? AND status = ?", Lead, true).order('id DESC').page(params[:page])
      @lead = Lead.find(params[:id])
      @lead.status = params[:status]
      @lead.save!
      respond_to do |format|
        format.js
 end 
[
{
"value": "ACURA",
"title": "Acura",
"models": [
{
"value": "CL_MODELS",
"title": "CL Models (4)"
},
{
@williamromero
williamromero / updating_other_user.md
Created November 2, 2017 17:24
Updating users in Devise without fuck up your current user data
def update
  respond_to do |format|
    # information. Devise solution: https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface
    if params[:user][:password].blank?
      params[:user].delete(:password)
      params[:user].delete(:password_confirmation)
    end        
    if @user.update_attributes(user_params)  
      format.html { redirect_to dashboard_path, notice: 'The user role was updated' }

else