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# algoritms# arrow-functions# javascriptintro# usefullinks# sample

Latest from community today

<?php

$names = ['Petro', 'oleg', 'zanar', 'yura', 'igor'];

sort($names);

function find($names, $search) {
    $count = count($names);
    
    $half = floor($count / 2);
    $element = $names[$half];
    
    if ($element === $search) return $element;
    if ($count === 1) return false; 
    
    if ($search > $element) {
        return find(array_slice($names, $half), $search);
    } else {
        return find(array_slice($names, 0, $half), $search);
    }

}

echo find($names, 'Petro');
<?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(" ") ); 


Featured content