Geolocation precision - javascript

I programmed this code but there is a problem, the code works but if I open it and don't open maps on my phone before opening script the location is not precise, but if I open maps(in maps I'm located with precise +/- 1 meter) than I get redirected accurately. Is there any way to put in my script that user is located precise like in maps?
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else {
alert("Vaš preglednik ne podržava geolokaciju.");
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("Molimo dopustite lociranje kako bi stranica pravilno radila.");
break;
case error.POSITION_UNAVAILABLE:
alert("Nema informacija o vašoj lokaciji.");
break;
case error.TIMEOUT:
alert("Vrijeme slanja zahtjeva isteklo.");
break;
case error.UNKNOWN_ERROR:
alert("Oops. Dogodila se nepoznata greška.");
break;
}
}
function showPosition(pos) {
//Neboder
if (pos.coords.latitude <= 45.77578 && pos.coords.latitude >= 45.77502 && pos.coords.longitude <= 15.995222 && pos.coords.longitude >= 15.994286)
{window.location = 'mob_test.html';}
//Vrtic
if (pos.coords.latitude <= 45.774885 && pos.coords.latitude >= 45.774754 && pos.coords.longitude <= 15.993956 && pos.coords.longitude >= 15.993886) {
window.location = 'vrtic.html';}
//Restoran
if (pos.coords.latitude <= 45.774896 && pos.coords.latitude >= 45.774748 && pos.coords.longitude <= 15.993792 && pos.coords.longitude >= 15.993671) {
window.location = 'skola.html';
}
//Ostalo
else {
window.location = 'bljak.html';
}
}

The HTML5 Geolocation API has options, which one of is enableHighAccuracy. Try setting that option to true and see if it makes any difference.

Related

alert dialog unable to close

I ran the following code in Google Chrome Version 54.0.2840.100 (64-bit). It displays an alert dialog successively asking for input until the user enters the correct answer, but the problem is that when i click the close button or cancel button in the alert dialog instead of closing, it continues asking for input.Also the close button on the tab is unclickable.Nevertheless I can close chrome main window but is there a code to correct this.
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
The easiest and straightforward answer would be to remove the do...while loop. 'coz that would keep the code running till you input the right answer.
So, you could check for the inputed number, if it is null break it, else convert it to your number and run the loop.
checking for null firsthand would make it easy to check for 0 as an input too
var answer = Number(Math.floor(Math.random()*10));
do{
var number = prompt("Guess a number");
if(number!=null){
number = Number(number);
} else {
break;
}
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
if(number === 0){ // this check is needed
break;
}
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
on closing the prompt, null is returned
passing null to Number function will return 0
hence the check for 0 is added
you could also modify the code as follows to allow 0 as input from the user
var input= prompt("Guess a number");
if(input === null){
break;
}
var number = Number(input);
... rest of the code
If click cancel, the input value will be null, then Number(null) will be 0.Unless the guess number is 0, the alert dialog would not be closed.
You'd better check the input value is null before cast it.
Window.prompt()
I have added a line of code to check whether user has cancelled the prompt.
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
// Add the below code to check for null and exit
if (number === null)break;
// END
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);

array within a switch javascript

A little while ago I needed help with a complicated switch, but now I am substituting the direct variables for arrays. I thought it best to use a for statement, and it isn't displaying the results.
This is a side project for me, so please let me know if there are any aspects you see that could be improved.
<script>
var strMod=0;
var dexMod=0;
var conMod=0;
var intMod=0;
var wisMod=0;
var chaMod=0;
var strength = prompt("what is your strength?");
var dexterity = prompt("what is your dexterity?");
var constitution = prompt("what is your constitution?");
var intelligence = prompt("what is your intelligence?");
var wisdom = prompt("what is your wisdom?");
var charisma = prompt("what is your charisma?");
var abilities=[strength,dexterity,constitution,intelligence,wisdom,charisma];
var abiMod=[strMod,dexMod,conMod,intMod,wisMod,chaMod];
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities(i)>=0 && abilities(i)<2 && abilities(i)!==null):
abiMod(i)=-5;
break;
case (abilities(i)>=2 && abilities(i)<4):
abiMod(i)=-4;
break;
case (abilities(i)>=4 && abilities(i)<6):
abiMod(i)=-3;
break;
case (abilities(i)>=6 && abilities(i)<8):
abiMod(i)=-2;
break;
case (abilities(i)>=8 && abilities(i)<10):
abiMod(i)=-1;
break;
case (abilities(i)>=10 && abilities(i)<12):
abiMod(i)=0;
break;
case (abilities(i)>=12 && abilities(i)<14):
abiMod(i)=1;
break;
case (abilities(i)>=14 && abilities(i)<16):
abiMod(i)=2;
break;
case (abilities(i)>=16 && abilities(i)<18):
abiMod(i)=3;
break;
case (abilities(i)>=18 && abilities(i)<20):
abiMod(i)=4;
break;
case (abilities(i)>=20 && abilities(i)<22):
abiMod(i)=5;
break;
default:
abiMod(i)= prompt("what is your"+ abilities(i) +"modifier?");
break;
};
alert(abiMod(i));
};
</script>
Rather than using the switch statement, there is a common theme across all case and so a mathematical formula can be used instead.
Here is the revised code
for (var i = 0; i < abilities.length; i++) {
if (abilities[i] !== null && abilities[i] >= 0 && abilities[i] < 22) {
abiMod[i] = Math.floor(abilities[i] / 2) - 5;
}
else {
abiMod[i] = prompt("what is your "+ abilities[i] +" modifier?");
}
};
For starters, you need to access array indices via brackets, not parens.
for (var i=0; i<abilities.length;i++){
switch(true){
case (abilities[i]>=0 && abilities[i]<2 && abilities[i]!==null):
abiMod[i]=-5;
break;
case (abilities[i]>=2 && abilities[i]<4):
abiMod[i]=-4;
break;
case (abilities[i]>=4 && abilities[i]<6):
abiMod[i]=-3;
break;
case (abilities[i]>=6 && abilities[i]<8):
abiMod[i]=-2;
break;
case (abilities[i]>=8 && abilities[i]<10):
abiMod[i]=-1;
break;
case (abilities[i]>=10 && abilities[i]<12):
abiMod[i]=0;
break;
case (abilities[i]>=12 && abilities[i]<14):
abiMod[i]=1;
break;
case (abilities[i]>=14 && abilities[i]<16):
abiMod[i]=2;
break;
case (abilities[i]>=16 && abilities[i]<18):
abiMod[i]=3;
break;
case (abilities[i]>=18 && abilities[i]<20):
abiMod[i]=4;
break;
case (abilities[i]>=20 && abilities[i]<22):
abiMod[i]=5;
break;
default:
abiMod[i]= prompt("what is your"+ abilities[i] +"modifier?");
break;
};
alert(abiMod[i]);
};
</script>

change html page according to day (mo, tu etc.)

window.onload=function(){
var day=new Date().getDay();
switch (day)
{
case 0:
window.document.location.href = 'su.html';
break;
case 1:
window.document.location.href = 'mo.html';
break;
case 2:
window.document.location.href = 'tu.html';
break;
case 3:
window.document.location.href = 'we.html';
break;
case 4:
window.document.location.href = 'th.html';
break;
case 5:
window.document.location.href = 'fr.html';
break;
case 6:
window.document.location.href = 'sa.html';
break;
}
};
it does not load the current html page. It loads the index.html file
can somebody pls tell me how to make this could work ?
thanks!
i've got an array whith week days
You can't put the javascript in every page, but only in a "loading" page that execute the code and navigate the user in the correct page.
Loading page (With your script) -> Go in the correct page
You will need to change your script to something like this if you want to have the same script in every page:
window.onload=function(){
var day=new Date().getDay();
var path = window.location.pathname;
var page = path.split("/").pop();
if(day == 0 && page != 'su.html')
window.document.location.href = 'su.html';
else if(day == 1 && page != 'mo.html')
window.document.location.href = 'mo.html';
else if(day == 2 && page != 'tu.html')
window.document.location.href = 'tu.html';
else if(day == 3 && page != 'we.html')
window.document.location.href = 'we.html';
else if(day == 4 && page != 'th.html')
window.document.location.href = 'th.html';
else if(day == 5 && page != 'fr.html')
window.document.location.href = 'fr.html';
else if(day == 6 && page != 'sa.html')
window.document.location.href = 'sa.html';
};

Boundary for canvas game in Javascript

I have to make a game for school with html5 canvas Javascript.
I am new to javascript and still learning but i really need some help with this issue that i have and would appreciate it if someone could help me out. I tried several things but nothing seems to work and im at a loss.
So this is the code for the player object. It can move from left to right.
Now the problem is that it leaves the canvas. I want it to stay in the canvas on the x axes.
// Things to do when keys are down
function onKeyDown(event) {
// prevent arrow keys from scrolling the page
if (event.keyCode >= 37 && event.keyCode <= 39) event.preventDefault();
switch (event.keyCode) {
case 37:
player.vx = -1;
player.image = player.imgLeft;
break; // left key
// case 38: player.vy = -1; break; // up key
case 39:
player.vx = 1;
player.image = player.imgRight;
break; // right key
}
}
// Things to do when keys are up
function onKeyUp(event) {
switch (event.keyCode) {
case 37:
case 39:
player.vx = 0;
player.image = player.original;
break; // left or right key released
// case 38: player.vy = 0; break; // up or down key released
}
}
This is what i got so far....
if ((player.x >= 800) && (player.x <= 0)) {
} else {
}
You could consider adding two functions to check that you're within bounds.
(Essentially the same as your code, albeit with my true condition returned in what would be your else statement.)
// returns true if param is in range [0..799]
function isInXrange(intPos)
{
if ((intPos>=0) && (intPos<800))
return true;
else
return false;
}
// returns true if param is in range [0..599]
function isInYrange(intPos)
{
if ((intPos>=0) && (intPos<600))
return true;
else
return false;
}
You could then add a function to move the player and another to handle colliding with the walls/wandering out of bounds
function movePlayer()
{
if (isInXRange(player.x))
player.x += player.vx;
if (isInXRange(player.y))
player.y += player.vy;
}
function handleOutOfBounds()
{
if (isInXRange(player.x) == false)
{
// do something;
}
if (isInYRange(player.y) == false)
{
// do something else
}
}
Basically just test to see if x + width (optional: + velocity) > canvas.width and the same for height.

Calling a method is giving me code and not the value (Javascript)

basically I have created an object with a method which adds several attributes in the object together. But when I try and call the method to the console log it fires out the code (which is a if statement) to me instead of the value which I was hoping it would return, so I am confused!
Why is this happening ?
Code Below:
var Granite = function(ty, gr, th, wi, le, ed, ad){
this.type = ty;
this.group = gr;
this.thickness = th;
this.width = wi;
this.length = le;
this.edgeProfile = ed;
this.addOns = ad;
this.groupPrice = function(){
if (thickness === 20){
switch(group)
{
case 1:
return 160;
break;
case 2:
return 194;
break;
case 3:
return 244;
break;
case 4:
return 288;
break;
case 5:
return 336;
break;
case 6:
return 380;
break;
default:
return 380;
}
}else{
switch(group)
{
case 1:
return 200;
break;
case 2:
return 242;
break;
case 3:
return 305;
break;
case 4:
return 360;
break;
case 5:
return 420;
break;
case 6:
return 475;
break;
default:
return 475;
}
}
}
this.price = function(){
if(length <= 2000 && length > 1000){
return ((edgeProfile + groupPrice)*2) - addOns;
}else if(length <= 3000 && length > 2000){
return ((edgeProfile + groupPrice)*3) - addOns;
}else if(length <= 4000 && length > 3000){
return ((edgeProfile + groupPrice)*4) - addOns;
}else if(length <= 5000 && length > 4000){
return ((edgeProfile + groupPrice)*5) - addOns;
}
}
}
var granite1 = new Granite("Rosa Porrino", 1, 30, 400, 3200, 30.05, 86.18);
console.log(granite1.groupPrice);
It returns the full if statement within the groupPrice method to me
You are not calling the method but providing reference of function to console,log(). In JavaScript you need use '()' to call a function.
this will surely work console.log(granite1.groupPrice());
In side this.price
use this.groupPrice(). instead of groupPrice
Modified this,price Method
this.price = function(){
if(length <= 2000 && length > 1000){
return ((this.edgeProfile + this.groupPrice())*2) - addOns;
}else if(length <= 3000 && length > 2000){
return ((this.edgeProfile + this.groupPrice())*3) - addOns;
}else if(length <= 4000 && length > 3000){
return ((this.edgeProfile + this.groupPrice())*4) - addOns;
}else if(length <= 5000 && length > 4000){
return ((this.edgeProfile + this.groupPrice())*5) - addOns;
}
}
If you're calling the function, append with () otherwise you're just referencing the function.

Categories