Heyy,I need help creating a javascript function without using arrays. Where the user needs to input a determined speed (S) at which a bird flies and then the distance to a determined tree (T) (for example T=13) If the user inputs 4, the bird flies for 4 miles 4 times ( for example if T=4, it lands on that tree), then it flies 3 miles 3 times, then 2 miles 2 meters and so on until 0. What i need to now is if the bird lands on the tree the user entered. I know I need to begin creating a
`function treeland (T,S)` {
}
let T= prompt("Enter T")
let S = prompt ("Enter S")
1 Like
How does a bird fly? Can you describe that again please?
When the user enters a number the birds flies for example if the user enters two and it begin in cero the bird will land on tree 2,4 and 5 another example can be if it enters 3 it will land on tree 3,6,9,11,13,14
Ok, so you have an example. In order to start turning that into code, we need to write it a bit more generally.
If I get a speed of n, what will my first n distances be?
n, n+n, n+n+n for example
Right, and how many times do you do this?
As many as the user enters, if it enters 4 the bird lands on 4,8,12,16,19,22,25,27,29,30
Ok, so how much do you know about loops? Could you make a loop to print out the just the first n values if a user enters n?
Cool, then could you make a loop to print out the just the first n values if a user enters n? This would be the first step in your solution.
for (let i=0;n>0;(n+n)*n)
I’m not sure that I understand your loop exit condition. And your loop body appears to be missing. You’ll need to put something in the loop body. For now I’d just log i
.
I dont understand what you mean sorry
for (let i=0;n>0;(n+n)*n)
This loop won’t run correctly. The exit condition and iterator is malformed, and there is no loop body.
for (let i = 0; /* exit condition */; /* iterator update */;) {
// loop body
}
n*n
isn’t an exit condition, and n+n
isn’t an iterator update. Here is a sample loop with valid syntax.
let n = 5;
// Loop head
// Initalize; Test; Update;
for (let i = 0; i < n; i = i+1;) {
// Loop body
console.log(i);
}
This loop will iterate from i = 0
to i = n - 1
and print i
on each iteration.
Thank you but I need the bird to fly at a speed and then determine if it lands on the tree
Right. So you will need to understand how to make a loop in order to be able to do so. Can you modify my loop to make it start at i = n
and print i
but increase by n
each time and stop when i > n*n
?