Draw path of divs between two divs [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to position n divs in between two absolute positioned divs? Im creating some sort of map and would like to draw footprints between two points. I was wondering if i could calculate coordinates of first and second div and determine how many footprints (with fixed width) would go in between and than somehow absolute position them with top and left position.
I don't know how to put this together in jQuery or Javascript.. Any ideas??
Thank you in advance!

Well, this is what I came up with:
Fiddle
There can be as many pairs of points as you wish, one has the class first, the other having the class second, and they will be paired with steps thanks to the following:
var f = $('.first');
var s = $('.second');
f.each(function(i) {
var f2 = f.eq(i);
var s2 = s.eq(i);
var xdist = s2.position().left - f2.position().left;
var ydist = s2.position().top - f2.position().top;
var dist = Math.sqrt(xdist*xdist + ydist*ydist); // Pythagoras
var numOfSteps = Math.round(dist / 50) - 1;
var stepX, stepY;
for (var l = 0; l < numOfSteps; l++) {
stepX = xdist/(numOfSteps+1)*(l+1) +
f2.width()/2 + f2.position().left;
stepY = ydist/(numOfSteps+1)*(l+1) +
f2.height()/2 + f2.position().top;
$('<div></div>').addClass('step').appendTo($(document.body))
.css('left', stepX + 'px')
.css('top' , stepY + 'px');
}
});
It's pretty much understandable without any comments, but if you can't figure out something, feel free to ask.
The number of footprints is calculated here:
var numOfSteps = Math.round(dist / 50) - 1;
and so to change the amount of footprints, change the number 50 to something else (the bigger the number, the less footprints).
Glad to help, was fun coming up with this ;)

Related

Something is wrong with my if and else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my code:
var points = 10
if(points == 10) {
points + 200;
}
else if (points === 100) {
points + 10;
}
console.log(points)
What happens is that it logs 10, when what i would like to happen is that it logs 210. Any idea on what i have done wrong? I got some feedback on it on an earlier question, but it still does not seem to work.
When we use assignment operator we have to use like this:
points = points + 200
so in your code at line "points + 200" or "points + 10" have not changed the value of variable points at all. So, variable points is the same as the first line (var points = 10)
You might modify your program like this:
var points = 10
if(points == 10) {
//points = points +200
points + 200;
}
else if (points === 100) {
//points = points + 10
points + 10;
}
console.log(points)

double loops stuck in forever loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to loop over my $('.fore-col') and iterate the index position of my api call variables each time incrementing upwards. Starting at 7 and going up by 8 until at the postion of 40. When i run this code, it is stuck in a "forever loop" i believe and didn't stop at the index position of 40. highest count before i pulled the plug was just over 20k.
Any help would be much appreciated!
url: forecastURL,
method: "GET"
}).then(function(res) {
console.log(res);
$(".fore-col").each(function() {
for (let i = 7; i < 40; i + 8) {
let date = res.list[i].dt_txt;
let icon =
"https://openweathermap.org/img/wn/" +
res.list[i].weather +
".png";
let temp = res.list[i].main.temp;
let humid = res.list[i].main.humidity;
console.log(date, icon, temp, humid);
}
});
```
enter code here
for (let i = 7; i < 40; i + 8) should be
for (let i = 7; i < 40; i = i + 8)

Formula - Cap width at 100% [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following working just fine. Anything under 100 is taken as itself while more than that is capped at 100. While it works fine, I would like to write it as a maths formula for neatness. Any ideas?
var ceiling = 100;
var incrementSize = 10;
var width = ""
if (score*incrementSize<ceiling)
{
width = "" + incrementSize * score + "%";
}
else
{
width = "" + ceiling + "%";
}
divj.style.width = width;
You could use a ternary operation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)
Basic syntax is:
("condition to be evaluated")? "return if true" : "return if false"
divj.style.width = (score*incrementSize<ceiling)? incrementSize * score + "%" : ceiling + "%";
This'll reduce things down to just one line.

Get the percentage of a number with javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I am creating a web app and I get some data.
There are points you have now (now=284), and total points (total=1000). So, The difference between them is dif=716.
How do I use javascript to turn the difference into a percentage value, for example, 32% or whatever?
Thanks
This is a math question, not programming, but you ask for javascript here you are an example:
var value = 249;
var total = 1000;
var calcPercent = function(v, t) {
return 100*v/t;
};
alert(calcPercent(value, total)+"%");
var total = 1000,
subtract = 284;
var differencePercentage = ((total - subtract) / total) * 100; // 71.6
Simple math would be
var now = 284;
var total = 1000;
console.log( "percentage is " + (now* 100/total) );
console.log( "Negative percentage is " + (100-(now* 100/total)) );
(function(){
var now = 284;
var total = 1000;
var difference = ((total - now) / total) * 100;
alert(difference);
})()

How do I make a numerical array's increase amount smaller in each step? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to make a numeric array which increases in every step.
[1,200,400,600,800,1000, .... , 10000]
But I need to make the amount by which it increases progressively smaller in every step. For example,
[1, 200, 300, 350, 325, 312.5, ....., 10000]
If anybody knows the solution, please give me some ideas.
Thank you.
Change the increment amount as you please...
var arr = [];
var i = 1;
var incrementAmt = 2000;
for(var j = 0; j < 1000; j++) {
var num = i + incrementAmt;
arr.push(num);
i = num;
i++;
incrementAmt = incrementAmt / 2; // cause incrementer to decrease each iteration
}
console.log(arr)

Categories

Resources