JavaScript Math functions examples
List of usefull JavaSript functions and constans
var 1_radian_to_degree = 180 / Math.PI; // 57,295779513082320876798154814105 degree
var 1_degree_to_radian = Math.PI / 180; // 0,01745329251994329576923690768489 radian
var 360_degree = 2 * Math.PI * 1_radian_to_degree;
parseInt("10.33") // returns 10 string to integer
parseFloat("10.33") // returns 10.33 string to float
// change sign
var a = -1;
a = a * -1; // +1
// random sign
a *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
var who_is = 58;
var type = (typeof who_is); // number
Math.E // natural logarithms, e, approximately 2.718281828459045
Math.LN10 // natural logarithm of 10, approximately 2.302585092994046
Math.LN2 // natural logarithm of 2, approximately 0.6931471805599453
Math.PI // approximately 3.141592653589793
Math.SQRT1_2 // square root of 1/2 which is approximately 0.7071067811865476
Math.SQRT2 // square root of 2, approximately 1.4142135623730951
Math.pow(4, 2) = 16 // 4 x 4 ... base 4 to the exponent 2 power
Math.abs(-5) absolute value of a number // return 5
Math.tan(degrees * Math.PI / 180) // get tangent from degrees
Math.tan(degree * Math.PI / 180)*adjacent = opposite
opposite / Math.tan(degree * Math.PI / 180) = adjacent
Math.acos(adjacent / hypotenuse) // angle of a right-angle triangle in radians
Math.atan(opposite / adjacent) // angle of a right-angle triangle in radians
var adjacent = Math.cos(radian) * hypotenuse; // leng of adjacent side
// find the point on a circle if you know the circle´s radius
var x = Math.cos(pointAngleInRadiansFromCentreOfCircle) * radius;
var y = Math.sin(pointAngleInRadiansFromCentreOfCircle) * radius;
Math.cbrt(64) // cube root of a number 4 (4 x 4 x 4)
Math.exp(xNumber) // returns e<sup>xNumber</sup> ... e == Math.E
Math.ceil() // always rounds a number up to the next largest integer
Math.ceil(1.001) // 2
Math.floor() // returns largest integer less than or equal to a given number
Math.floor(5.85) = 5
Math.floor(-5.15) = -6
const arrayA = [2, 3, 1];
Math.min(...arrayA) = 1;
Math.max(1, 3, 2) = 3;
Math.random() // returns 0.000.... - 0.99999999999
Math.random() * 3 // returns 0.000 - 2.99999999999
Math.round() // returns value of a number rounded to the nearest integer.
Math.round(0.9) // 1
Math.round(5.5) // 6
Math.round(5.05) // 5
Math.floor(Math.random() * 3) // returns 0, 1, 2
Date: 14.06.2020 - 08:3877LW NO topic_id
AD
Další témata ....(Topics)
Testování regulárních výrazů - regex
https://regex101.com/
Je možno zadat příchozí adresu a nechat jí upravit, pak znovu nechat projít
Zadá se adresa //bla.bl?query_string=testovany_string
Do velkého pole se zadají
RewriteCond
RewriteRule
a klikne se na TEST
https://htaccess.madewithlove.be/
Přesměrování stránek
https://www.sslmentor.cz/napoveda/presmerovani-https-pomoci-htaccess
Easily check status codes, response headers, and redirect chains.
Kontrola jak probíhá přesměrování s jednotlivými hlášeními - 301, 200, 404 atd.
https://httpstatus.io/
https://regex101.com/
Je možno zadat příchozí adresu a nechat jí upravit, pak znovu nechat projít
Zadá se adresa //bla.bl?query_string=testovany_string
Do velkého pole se zadají
RewriteCond
RewriteRule
a klikne se na TEST
https://htaccess.madewithlove.be/
Přesměrování stránek
https://www.sslmentor.cz/napoveda/presmerovani-https-pomoci-htaccess
Easily check status codes, response headers, and redirect chains.
Kontrola jak probíhá přesměrování s jednotlivými hlášeními - 301, 200, 404 atd.
https://httpstatus.io/
Adresa dynamická na statickou adresu
Více na: https://www.jakpsatweb.cz/server/mod_rewrite/mod-rewrite-priklady.html
RewriteEngine On
RewriteBase /
# //localhost/internet/download.php?ds=NECO zadana adresa
# QUERY_STRING je vse za otaznikem tedy ds=NECO
# kdyz query string zacina ds=(NECO) a uz za nim neni vubec nic, zadny dalsi &val=value ^&
RewriteCond %{QUERY_STRING} ^ds=([^&]+)$
# tak download.php nahradit za %1-download-data-link.html? pricemz %1 je to co se vybraloz za ds=, tedy NECO
# R=301 je presmerovani takze znovu to posle na web ke kontrole napr. do tohoto souboru kdyz je adresa na tento web
# L .. last .. posila adresu ke kontrole na web. adresa bude NECO-download-data-link.html
RewriteRule ^download\.php$ %1-download-data-link.html? [R=301,L,NE]
# pri dalsi kontrole prevede NECO-download-data-link.html zpet na download.php?rw=3&ds=NECO a uz to hleda v adresarich
# ale v adresnim radku prohlizece uz zustane ta prichozi NECO-download-data-link.html
#QSA prida query string, bude se pak jeste hodit. rw=3 muzeme pak odchytit v php ALE hlavne brani cykleni, jinak
# by to odchytil zase znovu RewriteCond %{QUERY_STRING} ^ds=([^&]+)$ kdyby tam zustal jen ds=NECO
RewriteRule ^([^/]+)-download-data-link\.html$ download.php?rw=3&ds=$1 [L,QSA]
Více na: https://www.jakpsatweb.cz/server/mod_rewrite/mod-rewrite-priklady.html
Tohle jsou některé metody objektu document
clear
createDocument
createDocumentFragment
createElement
createEvent
createEventObject
createRange
createTextNode
getElementById
/* nejpouzivanejsi metoda objektu document, ktera
ziska handle elementu dle jeho id*/
getElementByTagName
var e = document.getElementById('list');
e= e.getElementsByTagName('span');
i=0;
while(i < e.length){
e[i].style.color = "green";
}
write
document.write("some text");
Příklad vychází z předpokladu, že těsně před konečným
tagem /BODY je kontejner tag s názvem < footer>< /footer>.
Mezeru < TAGNAME nutno odstranit v reálném kódu.
Pokud tento tag je nalezen, lze předpokládat, že uživatel
stránku již uvidí celou, protože většina prohlížečů si s
tím poradí a doplní chybějící uzavírací tagy BODY a HTML.
// < TAGNAME remove space after < !!!!!
< BODY onload="myFunction">
< sript>
function myFunction() {
var x = document.getElementsByTagName ("footer");
if(x.length === 0)
alert("Page not loaded correctly ... not find tag footer x" + x);
else
alert("Heureka! :) FOOTER is loaded!");
}
< /script>
Pokud často pracujete v PHP se soubory, tak se Vám může hodit
tato funkce, která vrací obsah souboru, jako string, narozdíl od funkce
file(), která vrací obsah souboru, jako pole řádků.
tato funkce, která vrací obsah souboru, jako string, narozdíl od funkce
file(), která vrací obsah souboru, jako pole řádků.
/**
* function return file content as string
* $filename is path to file
*/
function getContents($filename) {
if (function_exists('file_get_contents')) {
return file_get_contents($filename);
} else {
$fp = fopen($filename, 'r');
if (!$fp) {
echo ('Could not read contents <br/>');
return false;
}
return fread($fp, filesize($filename)); // return string
}
}
/**
* function file return file content as an array of rows
*/
$rows = file($filename);
/**
* fopen() modes
*/
r Read
r+ Read and write, prepend
w Write, truncate
w+ Read and write, truncate
a Write, append
a+ Read and write, append
Editace: 20.6.2020 - 08:32
Počet článků v kategorii: 77
Url:javascript-math-functions-constans-examples