Skip to content

Instantly share code, notes, and snippets.

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

Razvan Zamfir Ajax30

🏠
Working from home
View GitHub Profile
@Ajax30
Ajax30 / addfunc.js
Last active September 5, 2020 11:26
Add all function argumens
const addUp = (...numbers) => {
var sum = 0;
numbers.forEach(function(item) {
sum = sum + item;
});
return sum;
}
console.log(addUp(1, 5, 6));
<?php
Class User {
public $first_name;
public $last_name;
public $email;
public function setUserData($first_name, $last_name, $email){
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->email = $email;
}
// Enter Mongo
Access `C:\Program Files\MongoDB\Server\4.2\bin` via CMD
// Enter mongo mode
mongo
// Show databases
show dbs
// Create and switch to database
@Ajax30
Ajax30 / Database
Last active October 2, 2018 07:24
Database class mysqli object oriented
<?php
class Database
{
public $host = DB_HOST;
public $username = DB_USER;
public $password = DB_PASS;
public $dbname = DB_NAME;
public $link;
public $error;
@Ajax30
Ajax30 / pdo_fetch_assoc.php
Last active July 16, 2018 15:39
Use PDO fetch method to display table rows
<?php
$sql = 'SELECT * FROM people';
$result = $db->query($sql);
$numrows = $result->rowCount();
?>
<table>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Gender</th>
<?php
try {
$con = new PDO('mysql:host=127.0.0.1;dbname=db_name','db_user','myPass');
} catch (PDOException $e) {
die("Database connection failed.");
}
?>
function addArrayMembers(arr) {
sum = 0;
arr.forEach(function(el, index) {
return sum = sum + arr[index];
});
return sum;
}
var arr = [1, 3, 5, 7];
addArrayMembers(arr);
@Ajax30
Ajax30 / factorial.js
Last active November 28, 2016 08:05
Calculate the factorial of a number passed as the single parameter of a function
function fact() {
arr = [];
var n = arguments[0];
if (n == 0) {
arr.push(1);
} else {
for (i = 1; i <= n; i++) {
arr.push(i);
}
}
@Ajax30
Ajax30 / addargs2.js
Created November 23, 2016 19:09
Add the arguments passed to a function 2
function add(){
// Create an array from the functions arguments object
// then sum the array members using reduce
var sum = Array.from(arguments).reduce(function(a, b){
return a + b;
});
console.log(sum);
}
// You can now add as many numbers as you like
// just by passing them to the function
@Ajax30
Ajax30 / addargs.js
Created November 12, 2016 07:26
Add ALL the arguments passed to a function
function add(){
var sum = 0;
// Use "the arguments object" to add up all the arguments
// passed to a function
for (i=0; i<arguments.length; i++){
var sum = sum + arguments[i];
}
console.log(sum);
}
// You can now add as many numbers as you like