+ 3

How to make JavaScript follow all lines of code sequentially, and make it wait for response from each previous line?

Just found out that JavaScript doesn't wait for responses before running next line. Example: Var score = getscore(); If (score>Bestscore){ //Something } Here the function getscore takes time to return a value, but that if statement doesn't wait for this. This is the actual code(Slightly different from example i mentioned above) https://code.sololearn.com/WHUUA0emqoRZ/?ref=app

17th Apr 2020, 6:27 AM
Omkar Bhale
Omkar Bhale - avatar
5 Antworten
+ 6
https://www.sololearn.com/post/170717/?ref=app https://code.sololearn.com/WKKkpq0efxai/?ref=app
17th Apr 2020, 8:16 AM
Gordon
Gordon - avatar
+ 2
I think you may have better chance for answer if you just share the link of a complete version of the code. It is unclear what `getScore` does, or its specification. In case you didn't know how to share links, just follow the guide below 👇 https://www.sololearn.com/post/74857/?ref=app
17th Apr 2020, 7:05 AM
Ipang
+ 1
Ipang it is actually a very lengthy program😅. I've done that on my computer. It is basically a minimax algorithm in a game, which the program uses to predict the best move from the computer side. What getscore(); does, is it loops through all possible moves the computer could do, and then passes the current game state(current position of the game) to another function. Let's say analysis(); (analysis function is called for each iteration of loop) (Which also means that the loop should wait, till analysis () returns the score) Damn this JavaScript(in non offensive way) Analysis then calculates score depending on game state, and returns score for each of them to getscore() function. Lets say this score is stored in a array created in getscore(). Which in turn returns the array. I hope i made it clear enough🙄 note. i tried to simplify and tell the working, it works a little complicated. (search for minimax algorithm for more) p.s. i can get that code here on solo learn if you want
17th Apr 2020, 9:35 AM
Omkar Bhale
Omkar Bhale - avatar
+ 1
Omkar Bhale Thanks for elaborating buddy. Summing up on your description, I don't suppose the function is asynchronous, as you said, it does calculations to deduct feasible moves for the computer and analyse game state and does score calculation. All this are supposed to be done before control is returned to getscore(). I guess it would be great if you could share the code here. Though I don't know if I could help, as I'm on mobile which is rather inconvenient for reading large codes, but I might try. There are plenty of webmasters here, even if I couldn't, I'm pretty sure there will be someone who can help with that 👍
17th Apr 2020, 1:06 PM
Ipang
+ 1
Ipang I have attached the exact code i have written on my PC.
17th Apr 2020, 4:53 PM
Omkar Bhale
Omkar Bhale - avatar