Best short notes on programming concepts & questions asked in interviews

Share sandboxes with short explanation with the community

Post your first short note
# All# javascript# hoisting# gambling# primitives# arrow-functions# javascriptintro# usefullinks

Latest from community today

<?php

const MAX_ROUND = 100, MUL = 2,CREDIT_INIT = 200;
$current_balance = CREDIT_INIT;$bet_amount = 1;
$win_count = 0;$lose_count = 0;
$turn_over = 0;$total_bet_win = 0;$total_bet_lose=0;


for($i = 0;$i<MAX_ROUND;$i++)
{
    $result = mt_rand()%2;$turn_over += $bet_amount;
  
  	if($result == 1)
    {
      $current_balance = $current_balance+$bet_amount;$total_bet_win += $bet_amount;
      $win_count++; $bet_amount = 1;     
    }
  	
  	else
    {
      $current_balance = $current_balance-$bet_amount;$total_bet_lose += $bet_amount;
      $lose_count++;$bet_amount = $bet_amount*MUL; 

    }
  
  
  
        $result = $result == 1 ? 'win' : 'lose';
    	echo "round :".(1+$i)." result: ".$result ."(W/L) :"."(".$win_count."/".$lose_count.")";
        echo " ,bet_amount :".$bet_amount." ,balance : ".$current_balance."\n";
}
		echo "\n\n|___________________SUMMARY________________|\n";
		echo "Turnover: "."$turn_over". "\nTotal Win: ".$total_bet_win."\nTotal Lose: ".$total_bet_lose;
		echo "\nProfit: ". ($current_balance - CREDIT_INIT);



<?php


$current_balance = 200;
$bet_amount = 1;

const MAX_ROUND = 500;

for($i = 0;$i<MAX_ROUND;$i++)
{
    
	$result = mt_rand()%2;
  
  

  
  	if($result == 1)
    {
      $current_balance = $current_balance+$bet_amount;
      $bet_amount = 1;      
    }
  	
  	else
    {
      $current_balance = $current_balance-$bet_amount;
      $bet_amount = $bet_amount*2;      

    }
  
        $result = $result == 1 ? 'win' : 'lose';
    	echo "result: ".$result ;
        echo " ,bet_amount :".$bet_amount;
  		echo " ,balance : ".$current_balance."\n";

  
 
  
}



What javascript cannot do:

- Cannot read files on the hardisk

- Things like, camera, microphone, location can be accessed via javascript but only by taking permission from the user

- Windows from two different domains cannot know about each other. This is called the "Same Origin Policy"

- Cross origin requests from one domain to another are not allowed unless the other domain allows it explicitly or a separate routing is setup.
/**
There’s another very simple and concise syntax for creating functions, that’s often better than Function Expressions.

It’s called “arrow functions”, because it looks like this:
*/
let func = (arg1, arg2, ...argN) => expression


// Here is non-arrow version of the above function
let func = function(arg1, arg2, ...argN) {
  return expression;
};



let sum = (a, b) => a + b;

/* This arrow function is a shorter form of:

let sum = function(a, b) {
  return a + b;
};
*/
console.log( sum(1, 2) ); // 3




let double = n => n * 2;
// roughly the same as: let double = function(n) { return n * 2 }

console.log( double(3) ); // 6



let sayHi = () => console.log("Hello!");

sayHi();






let sum = (a, b) => {  // the curly brace opens a multiline function
  let result = a + b;
  return result; // if we use curly braces, then we need an explicit "return"
};

console.log( sum(1, 2) ); // 3
console.log( "6" / "2" ); // 3, strings are converted to numbers




let str = "123";
console.log(typeof str); // string

let num = Number(str); // becomes a number 123

console.log(typeof num); // number




let age = Number("an arbitrary string instead of a number");

console.log(age); // NaN, conversion failed



console.log( Number("   123   ") ); // 123
console.log( Number("123z") );      // NaN (error reading a number at "z")
console.log( Number(true) );        // 1
console.log( Number(false) );       // 0




console.log( Boolean(1) ); // true
console.log( Boolean(0) ); // false

console.log( Boolean("hello") ); // true
console.log( Boolean("") ); // false



console.log( Boolean("0") ); // true
console.log( Boolean(" ") ); 


/**

HOISTING

*/
var x = "global scope";
function foo() {
  var x = "functional scope";
  console.log(x);
}
foo(); // "functional scope"
console.log(x); // "global scope"


// What if we were to drop the second var
var y = "global scope";
function bar() {
  y = "functional scope";
  console.log(y);
}
bar(); // "functional scope"
console.log(y); // "functional scope"


/**

SCOPE

*/
var a = 0; 
let b = 0;
const c = 0;
if (true) {
  var a = 1;
  let b = 1; 
  const c = 1;
}
console.log(a); // 1
console.log(b); // 0
console.log(c); // 0

Featured content