Skip to content

Instantly share code, notes, and snippets.

View DavidMellul's full-sized avatar

David Mellul DavidMellul

View GitHub Profile
let x = [1,2,3,4,5];
let y = [];
for(let i = 0; i < x.length; i++)
if(x[i] % 2 == 0)
y.push(x[i]*2);
else
continue;
console.log(y);
let x = [1,2,3,4,5];
let y = x.map ( element => element*2 );
console.log(y);
// => [2,4,6,8,10]
let x = [1,2,3,4,5];
let y = [];
for(let i = 0; i < x.length; i ++)
y[i] = x[i]*2;
console.log(y);
// => [2,4,6,8,10]
let x = 5;
let expected = 8;
console.log(`Content of variable x is: ${x} and expected is ${expected}`);
// => Content of variable x is: 5, and expected is 8
let x = 5;
let expected = 8;
console.log('Content of variable x is: ' + x + ', and expected is ' + expected);
// => Content of variable x is: 5, and expected is 8
const PRECISION = 0.2; // Often called a Delta
function isValid(time) { // Ensures 1 second has elapsed
const expected = 1;
// We only assert this : (expected - precision) <= time <= (expected + precision)
return (expected - PRECISION <= time) && (expected + PRECISION >= time);
}
console.log(isValid(1.105));
var torchRunning = false;
var startTime = new Date();
function sleep(milliseconds) { // A basic sleep implementation to "pause" the current thread
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
var lightPlugged = false;
function switchLight()
{
lightPlugged = !lightPlugged;
}
console.log( lightPlugged ? 'Light is on':'Light is off');
// => Light is off
var lightPlugged = false;
function switchLight()
{
if(lightPlugged == false)
lightPlugged = true;
else
lightPlugged = false;
}
function databaseCrashed() {
return true;
}
function leaveBuilding() {
console.log('We are safe');
}
function stayInBuilding() {
console.log('We are working hard in here');