Skip to content

Instantly share code, notes, and snippets.

View Elijah-trillionz's full-sized avatar
💭
hacking

Elijah Trillionz Elijah-trillionz

💭
hacking
View GitHub Profile
@Elijah-trillionz
Elijah-trillionz / range.js
Created October 26, 2021 18:35
Just like python range
function range(start, end, step = 1) {
const rangeOfNums = []
if (step < 0) {
for (let i = start; i >= end; i -= Math.abs(step)) {
rangeOfNums.push(i);
};
} else {
for (let i = start; i <= end; i += step) {
rangeOfNums.push(i);
};
@Elijah-trillionz
Elijah-trillionz / arrays.js
Created October 26, 2021 18:34
Reversing javascript arrays without the reverse method
// reverse an array that modifies the array
function reverseArrayInPlace(arr) {
let length = arr.length;
for (let i = length - 1; i >= 0; i--) {
arr.push(arr[i]);
}
arr.splice(0, length);
return arr;
}
@Elijah-trillionz
Elijah-trillionz / lists.js
Created October 26, 2021 18:31
JavaScript lists
// convert from array to list with
function arrayToList(arr) {
// list format: value 1, rest => value 2, rest => value 3, rest => null
let list;
for (let i = arr.length; i >= 0; i--) {
list = {
value: arr[i],
rest: i >= arr.length - 1 ? null : list,
};
}
@Elijah-trillionz
Elijah-trillionz / file_handling.py
Created September 18, 2021 18:31
Handling files in php
# TODO: python delete file and folders
import os
# create file
html = open('demo.html', 'x')
html.close()
# write to new file
html = open('demo.html', 'w')
html.write('<html>\n<body>\n<p>\nHello World</p>\n</body>\n</html>')
@Elijah-trillionz
Elijah-trillionz / reverse.js
Created June 22, 2021 20:53
Reverse a number mathematically
// reversing a number
let givenNum = 4552;
let numInRev = 0;
while (givenNum > 0) {
const lastDigit = Math.floor(givenNum % 10);
numInRev = numInRev * 10 + lastDigit;
givenNum = Math.floor(givenNum / 10);
}
@Elijah-trillionz
Elijah-trillionz / reverse.js
Created June 15, 2021 20:05
Reverse a number in JavaScript
function reverseNumber(givenNum) {
const numInArr = givenNum.toString().split('');
numInArr.reverse();
return +numInArr.join('');
}
@Elijah-trillionz
Elijah-trillionz / palindrome.js
Created June 15, 2021 20:04
Palindrome Checker LVL 1
function checkForPalindrome(phrase) {
const phraseInArr = phrase.split('');
const noWhiteSpace = phraseInArr.filter((phrase) => {
return phrase !== ' '; // you can use regex to ignore characters
});
const toUseLength = Math.floor(noWhiteSpace.length / 2);
const firstSet = noWhiteSpace.slice(0, toUseLength).join('').toLowerCase();
@Elijah-trillionz
Elijah-trillionz / cookie.js
Last active May 22, 2021 13:54
Get Data From Cookies with one function
@Elijah-trillionz
Elijah-trillionz / spreadArray.js
Last active July 1, 2021 09:16
Spread Operators with Arrays
// spread operators allows any iterable to be expanded
// it can serve as a means of adding arrays (concatenation)
const arr1 = [1, 3, 4]
const arr2 = [10, 13, 5]
const bothArr = [...arr1, ...arr2]
// you can use this to update an array with
// another array without leading to a multidimensional array
let arr1 = [1, 3, 4]
const arr2 = [10, 13, 5]
@Elijah-trillionz
Elijah-trillionz / objects.js
Created April 10, 2021 20:37
Switching values of the same properties in two different objects
// i still feel like there is a better way to do this, but this is what i came up with. If you know a better way please share
const oldObject = { name: 'John Doe', nationality: 'South Africa' };
const newObject = { name: 'John Doe Seth' };
function changeDataInObjects(newData, oldData) {
for (let i in oldData) {
for (let j in newData) {
if (i === j) { // i and j represents the property names of oldData and newData respectively
oldData[i] = newData[j];
} else {