Skip to content

Instantly share code, notes, and snippets.

View ossi1801's full-sized avatar
:shipit:

Ossi H ossi1801

:shipit:
View GitHub Profile
@ossi1801
ossi1801 / array-sum.js
Created January 25, 2021 19:03
Sum of array, JavaScript
var arr = [1, 2, 3, 4, 5];
function sumArray(array){
var sum = array.reduce(function(a, b){
return a + b;
}, 0);
return sum;
}
sumArray(arr);
@ossi1801
ossi1801 / emailregex.js
Created January 27, 2021 17:10
A short email validation with regex in javascript
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
@ossi1801
ossi1801 / test_class.py
Created February 5, 2021 22:15
Short class tester for pytest
# Using pytest tool
# $ pip install -U pytest
# $ pytest test_class.py -v
# https://docs.pytest.org/en/stable/usage.html
class TestClass:
def test_one(self):
x = "this"
assert ("h" in x)
@ossi1801
ossi1801 / operators.bat
Created February 6, 2021 20:19
Bash operators
A; B # Run A and then B, regardless of success of A
A && B # Run B if and only if A succeeded
A || B # Run B if and only if A failed
A & # Run A in background.
@ossi1801
ossi1801 / numberToAccountingString.js
Last active February 12, 2021 13:41
Number to accounting string - Alternative solution
//https://github.com/WebDevSimplified/Noob-Vs-Pro-Code/blob/master/1-logic/3-pro.js
/* Rather than checking for null which does not factor that if the value is undefined, you can check for if value is true.
* This way the returned value is always either a 0 , or a positive number (or pos. n with parentheses)
* Is this a "proper solution" to this problem(?) maybe...
*/
function numberToAccountingString(number){
if( !number) return '0'
if (number < 0) return `(${Math.abs(number)})`
@ossi1801
ossi1801 / weekday.js
Created February 25, 2021 20:58
Check for weekday
const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 1, 25)));
// Result: true (Thurday 25.2.21)
console.log(isWeekday(new Date(2021, 1, 28)));
// Result: false (Sunday 28.2.21)
console.log(isWeekday(new Date()),new Date())
//Todays date
@ossi1801
ossi1801 / modulo.cs
Created July 12, 2021 13:23
Modulo test
using System;
public class Program
{
public static void Main()
{
int a= 21;
int b =0;
if (a%4!=0) b++;
var c = (a/4)+b;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
@ossi1801
ossi1801 / AccurateFloatAddition.js
Created December 1, 2021 15:41
//Adds floats together with "precision"
//Adds floats together with "precision"
var RoundedAddition = (...args) => {
let countDecimals = (value) => {
if (Math.floor(value) === value) return 0;
let a = value.toString().split(".")[1].length || 0;
return a;
}
let highestDecimalpoint;
args.forEach(i => {
let x = countDecimals(i);
@ossi1801
ossi1801 / CustomButton.cs
Created January 14, 2023 17:22
WinForms rounded borders button - Custom Button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;