Skip to content

Instantly share code, notes, and snippets.

@mrwanashraf
mrwanashraf / change-language-to-gdscript-on-github.sh
Created August 26, 2020 20:35
make github detect gdscript in the languages section
### make github detect gdscript in the languages section
# navigate to path where your godot project is
> 🐧Linux/Unix, MacOS
echo "*.gd linguist-language=GDScript" > .gitattributes
> 🪟 Windows
1- create a file named .gitattributes inside godot project
@mrwanashraf
mrwanashraf / passByValueVsPassByReference.js
Created May 4, 2020 09:17
when passing variables as parameters in a function you should pass by reference when the variable is expected to change otherwise pass by value. note that you need to pass the object itself not its value when passing by reference.
/* pass by value ⏳
*****************/
function hello (person) {
console.log(`Hello ${person}`);
}
function enterName(person, name) {
person = name;
console.log(person); // Mundo
}

Keybase proof

I hereby claim:

  • I am mrwanashraf on github.
  • I am mrwanashraf (https://keybase.io/mrwanashraf) on keybase.
  • I have a public key ASDCC-Ooqn9LMgDY6qtjCmPrpGoS87nA8Kax9dNowqKjwQo

To claim this, I am signing this object:

@mrwanashraf
mrwanashraf / flatten.js
Created August 26, 2019 22:42
flat a nested integer array
const array = [[1,2,[3]],4];
const flatten = (arr) => arr.toString().split(",").map( n => Number(n));
console.log(flatten(array));
@mrwanashraf
mrwanashraf / kill-ports.sh
Created August 13, 2019 09:14
sometimes stopping a dev server doesn't free the port that the server is using. here is how to list used ports and how to kill a specific port in that context using linux.
#!/bin/bash
# 💡 Search for busy ports
netstat -lnp
# ✋ kill a specific port
fuser -k portNumber/tcp
# 💡 example
fuser -k 8080/tcp
@mrwanashraf
mrwanashraf / selectClasses.js
Last active July 29, 2019 11:29
here are the 2 ways of how you can use to select elements based on their class in DOM using vanillajs the difference between them is that: - querySelectorAll returns a NodeList - while getElementsByClassName returns HTMLCollection
// unordered lists
<ul>
<li class="numbers">1</li>
<li class="numbers">2</li>
<li class="numbers">3</li>
<li class="numbers">4</li>
</ul>
// ✅ using querySelectorAll
@mrwanashraf
mrwanashraf / selectID.js
Created July 28, 2019 20:46
here are the 3 different ways you can use to select an element based on its ID in DOM using vanillajs
// h1 element
<h1 id="hello-world">Hello World</h1>
// ✅ using querySelector
document.querySelector("#hello-world");
// ✅ using getElementById
document.getElementById("hello-world");
@mrwanashraf
mrwanashraf / match-vs-if-let.rs
Created July 27, 2019 16:53
if let is syntax sugar for match in rust and you can achieve the same result with using any of them.
fn main() {
// Match
let mut hello_world = String::from("hello");
match hello_world.as_ref() {
"hello" => hello_world.push_str(" world"),
_=> hello_world.push_str("hello world")
}
println!("{}", hello_world); // will print hello world
@mrwanashraf
mrwanashraf / appendChild-vs-insertAdjacentHTML.js
Created July 27, 2019 14:28
appendChild is the default way to insert elements but unfortunately it only works with DOM elements, you can't use HTML strings with it. on the other hand insertAdjacentHTML allows you to do that.
const body = document.querySelector('body');
const helloWorldHeader = '<h1>Hello World</h1>';
// ❌ this won't work as appendChild only works with Elements not HTML
body.appendChild(helloWorldHeader);
// ✅ this will work as insertAdjacentHTML works very well with HTML
body.insertAdjacentHTML('afterbegin', helloWorldHeader);
@mrwanashraf
mrwanashraf / innerHTML-vs-insertAdjacentHTML.js
Created July 26, 2019 14:57
innerHTML doesn't work well with the DOM specially when using loops, as it overwrites the content. using insertAdjacentHTML is one of the ways you can use to eliminate unnecessary bugs related to DOM when using vanilla js.
const body = document.querySelector('body');
const helloWorld = "<h1>Hello world</h1>";
// ❌ Don't use innerHTML as it messes with DOM
body.innerHTML = helloWorld;
// ✅ Do use insertAdjacentHTML as it goes well with DOM without messing it
body.insertAdjacentHTML('afterbegin', helloWorld);