Skip to content

Instantly share code, notes, and snippets.

View bryancodesjs's full-sized avatar

Bryan bryancodesjs

View GitHub Profile
@bryancodesjs
bryancodesjs / rosniel.html
Created July 11, 2023 22:20
Compartiendo ejercicio de bucle usando .map()
<html>
<head>
<style>
ul {
list-style: none;
margin:0;
padding: 0;
}
img {
const mongoose = require('mongoose');
const DB_PORT = 27017;
const DB_NAME = 'playground';
mongoose.connect(`mongodb://localhost:${DB_PORT}/${DB_NAME}`)
.then(() => console.log('Connected to Mongo...'))
.catch(err => console.error('Could not connect...', err))
const courseSchema = new mongoose.Schema({
name: String,
author: String,
@bryancodesjs
bryancodesjs / useReduce.js
Created March 8, 2023 03:49
A brief example of how Array.prototype.reduce() can be used
const transactions = [
{ id: 1, amount: 100 },
{ id: 2, amount: 200 },
{ id: 3, amount: 300 },
{ id: 4, amount: 450 }
]
const useReduce = transactions.reduce((acc, curr) => {
return acc + curr.amount;
},0);
@bryancodesjs
bryancodesjs / disk-space-hackerrank.js
Created February 6, 2022 04:18
my solution to hackerrank hard disk drive challenge
let space = [2,3,1,5,3,9];
let x = 2;
let results = [];
function calculateMaxDisk(seg, arr)
{
//calculate amount of modules
let module = ((arr.length) - seg) + 1;
console.log('this function will create ' + module + " iterations!");
//main loop, creates 'x' amount of iterations
for( let i=1 ; i <= module ; i++ )
@bryancodesjs
bryancodesjs / returnDescendingOrder.js
Created January 30, 2022 04:05
take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
//this is an example integer
const myInt = 52132;
function descendingOrder(n) {
if (n > -1) {
let toArr = Array.from(String(n), Number);
let sorted = toArr.sort((a, b) => a - b).reverse();
let joined = sorted.join("");
return parseInt(joined);
}
@bryancodesjs
bryancodesjs / fetch.js
Created January 22, 2022 03:26
Fetch data from an API and appending the results to an unordered list
//this is a mock API I made for the occasion
const submitUrl = 'https://retoolapi.dev/QqocMc/data';
//this selects the unordered list with the id 'list' on the html template
//you should have that set up beforehand
const htmlListNode = document.getElementById('list');
//Use the fetch method and pass it the url of the API
fetch(submitUrl)
.then(res => res.json()) //parse the response and then...
.then(data => {
let length = data.length; //save the length of the array to use it as a reference in the loop
@bryancodesjs
bryancodesjs / retrieveAndIterate.js
Created January 21, 2022 20:02
retrieve data from an API using XMLHttpRequest and then iterate over the results
//API url
const quotesUrl = 'https://quote-garden.herokuapp.com/api/v3/quotes'
//my function
function myRequest() {
//initialize an instance of the XMLHttpRequest object
const req = new XMLHttpRequest();
//set the parameters to be used in the get request
req.open("GET", quotesUrl, true);
//send the get request
@bryancodesjs
bryancodesjs / fetchRequest.js
Created January 21, 2022 17:58
how to use Fetch to retrieve data from an API
//For this gist, I'm using a free, public API that shows information about a user's location based on their IP address
//here's where I store the API key I was provided with
const myKey = 'c00508dc4104c70518fcf49e17216676';
//here's a random IP from the internet
const myIp = '191.101.63.2';
//here's the base url
const myUrl = 'http://api.ipstack.com/';
function myRequest() {
@bryancodesjs
bryancodesjs / XMLHttpRequest.js
Created January 21, 2022 17:45
How to make an XML HttpRequest
//For this gist, I'm using a free, public API that shows information about a user's location based on their IP address
//here's where I store the API key I was provided with
const myKey = 'c00508dc4104c70518fcf49e17216676';
//here's a random IP from the internet
const myIp = '191.101.63.2';
function myRequest(){
//initialize the XMLHttpRequest object
const request = new XMLHttpRequest();
@bryancodesjs
bryancodesjs / find-multiple.js
Last active January 20, 2022 04:28
Count from 1 to 100 and print a message every time a multiple of 3 or 5 is found.
function findMultiples() {
let baseNumer = 0;
for(let i = 0; i<100 ; i++) {
baseNumer ++;
console.log(baseNumer);
if(baseNumer % 3 == 0) {
console.log('multiple of 3 found');
} else {
if (baseNumer % 5 == 0) {
console.log('multiple of 5 found');