//let numbers = [0.3, 0.5, 0.6, 0.8, 0.9, 2.1, 3.4]; // input
//let numbers = [0.7, 0.7, 0.8];
//let numbers = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1];
//let numbers = [3.7, 1.7, 0.8];
let numbers = [0.5, 7.6, 47.4];
function roundNumbers(input) {
let flooredNumbers = input.map(function(a){return Math.floor(a)});
let sumLeft = Math.round(sum(input)) - sum(flooredNumbers);
let sorted = [];
input.forEach(function(a,i){
sorted.push({
idx: i,
elm: (a-Math.floor(a))
})
});
sorted.sort(function(a,b){ return b.elm - a.elm});
let i = 0;
while(i < sumLeft) {
flooredNumbers[sorted[i].idx] += 1;
i++;
}
return flooredNumbers;
}
function sum(arr) {
return arr.reduce((a, b) => a + b, 0);
}
roundNumbers(numbers);
<p id="myid">some content</p>
#myid::after {
display:block;
width: 100px;
height: 40px;
color: blue;
content: "Hello world";
}
#myid {
color: red;
}
var color = window.getComputedStyle(document.querySelector('#myid'), ':after').getPropertyValue('color')
var content = window.getComputedStyle(document.querySelector('#myid'), ':after').getPropertyValue('content')
console.log(color, content) // outputs the color and content of the pseudo-element
/**
* @description URL parsing using native JS engine approach
* @param {string} url - URL string
* @returns {object} parsed data
*/
function parseURL(url) {
var parser = document.createElement('a');
parser.href = url;
return {
protocol : parser.protocol, // => "http:"
hostname : parser.hostname, // => "domain.com"
port : parser.port, // => "8000"
pathname : parser.pathname, // => "/path/"
search : parser.search, // => "?search=test"
hash : parser.hash, // => "#hash"
host : parser.host // => "domain.com:8000"
}
}
console.dir(parseURL("http://domain.com:8000/path/?search=test&bar=1&bar=2&key1=val1#hash"));
cat /proc/`pidof node`/status | grep Threads
ls /proc/19899/task/ | wc -l
ps hH p `pidof node` | wc -l
require('os').arch()
process.arch
process.env.PROCESSOR_ARCHITECTURE
var path = require('path');
module.exports = function (mongoose) {
var Schema = new mongoose.Schema({
name: {type: String, trim: true, required: true},
uuid: {type: String, required: true, unique: true, index: true},
device: {type: mongoose.Schema.Types.Mixed},
updated_at: {type: Date, default: Date.now}
}, {autoIndex: true});
return mongoose.model(path.basename(module.filename, '.js'), Schema);
};
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.someWebApiServer.com/some-data';
<script>
{['some string 1', 'some data', 'whatever data']}
</script>
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.someWebApiServer.com/some-data?callback=my_callback';
my_callback({['some string 1', 'some data', 'whatever data']});
<html>
<head>
</head>
<body>
<div id = 'twitterFeed'></div>
<script>
function myCallback(dataWeGotViaJsonp){
var text = '';
var len = dataWeGotViaJsonp.length;
for(var i=0;i<len;i++){
twitterEntry = dataWeGotViaJsonp[i];
text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
}
document.getElementById('twitterFeed').innerHTML = text;
}
</script>
<script type="text/javascript" src="http://twitter.com/status/user_timeline/padraicb.json?count=10&callback=myCallback"></script>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: 'http://twitter.com/status/user_timeline/padraicb.json?count=10',
dataType: 'jsonp',
success: function(dataWeGotViaJsonp){
var text = '';
var len = dataWeGotViaJsonp.length;
for(var i=0;i<len;i++){
twitterEntry = dataWeGotViaJsonp[i];
text += '<p><img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>'
}
$('#twitterFeed').html(text);
}
});
})
</script>
</head>
<body>
<div id = 'twitterFeed'></div>
</body>
</html>
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -all s,l,u; killall Finder
var fib = function (_) {
for(_=[+[],++[[]][+[]],+[],_],_[++[++[++[[]][+[]]][+[]]][+[]]]=(((_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]]))&(((--[[]][+[]])>>>(++[[]][+[]]))))===(_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])))?(_[++[++[[]][+[]]][+[]]]=++[[]][+[]],_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])):+[];_[++[++[++[[]][+[]]][+[]]][+[]]]--;_[+[]]=(_[++[[]][+[]]]=_[++[++[[]][+[]]][+[]]]=_[+[]]+_[++[[]][+[]]])-_[+[]]);
return _[++[++[[]][+[]]][+[]]];
}
console.assert(fib(-1) === 0);
console.assert(fib(0) === 0);
console.assert(fib(1) === 1);
console.assert(fib(2) === 1);
console.assert(fib(3) === 2);
console.assert(fib(4) === 3);
console.assert(fib(5) === 5);
console.assert(fib(6) === 8);
console.assert(fib(7) === 13);
console.assert(fib(32) === 2178309);
console.assert(fib(46) === 1836311903);
console.assert(fib(47) === 2971215073);
console.assert(fib(63) === 6557470319842);
console.log('done!');
var someVar = 0;
alert(someVar == false); //evaluates true
alert(false == false); //evaluates true, of course
alert(someVar === false); //evaluates false - someVar is a number, not a legitimate boolean