Skip to content

Instantly share code, notes, and snippets.

View cmgdragon's full-sized avatar
🐲

cmgdragon

🐲
View GitHub Profile
@cmgdragon
cmgdragon / simple_base10_exchange.js
Created September 10, 2022 21:18
A couple of functions to turn any base 10 number to binary or hexadecimal
function dec_to_bin(num) {
const bin = [];
while (num >= 1) {
bin.push(Math.floor(num%2));
num = Math.abs(num/2);
}
bin.reverse();
console.log(bin.join(''));
@cmgdragon
cmgdragon / to_square.js
Last active July 30, 2022 21:37
A function that graphically represents the square of a number (made with three.js)
//TEST IT: https://jsfiddle.net/nkt9wd28/
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
@cmgdragon
cmgdragon / startTurnBasedGame.js
Last active July 29, 2022 07:08
A function for starting a game based in turns and rounds. You can select the initial player and the starting round. Made for practicing remainders
startTurnBasedGame(5, 3, 4);
function startTurnBasedGame(num_players, num_rounds, turn_time, init_player=1, init_round=1) {
let [elapsed_time, player_turn, round, actual_player] = [0, 0, init_round, init_player-1];
const getPlayer = () => actual_player === num_players ? num_players : actual_player % num_players;
const printTimeInfo = remaining_time => console.log(
`Next turn in: ${remaining_time}s`, `Player: ${getPlayer()}`, `Round: ${round}/${num_rounds}`
);
@cmgdragon
cmgdragon / fromObjectToJson.js
Last active April 19, 2024 23:27
A script for converting objects to json format, like the JSON.stringify function. Made for a class exercise
FromObjectToJson = object => {
try {
if (typeof object !== "object")
throw "It has to be an object!";
let json, cierre;
if (!Array.isArray(object)) {
json = "{";