How to repeat the loop if the condition is not met?
Good day. I decided to write an algorithm that will display the numbers in the array in ascending order. It works, but the problem is that it does 1 pass, not as much as needed. There was a thought to make this loop a function and add an array check and then run it again if necessary, but if you do this, the code will turn out to be too large for such a task. I have been learning JS for only a few days, please do not throw slippers. I want to learn how to write algorithms and wiggle my brains without looking at other algorithms like a bubble, etc. Is there a way to do this without a ton of code?
I want to learn how to write algorithms and wiggle my brains without peeping
To learn how to wiggle your brains, you first need to literally wiggle them (I'm hinting at independence). You contradict yourself: I want to learn to wiggle my brains without peeping into other people's algorithms, I'll go and ask on the toaster!
Write a function and call it recursively as needed is the first thing that comes to mind. To independently come up with something different, you need to either sit stupidly on one problem for an irrational time, or practice (this is the word to which I incline you) whatever. As experience grows, other independent solutions will come.Anonymous
There was a thought to make this loop a function and add a check on the array and then run it again if necessary
So do it. There is a While loop for this, it will call your function until the condition is metAnonymous
You can call recursion. That is, a function within itself. In general, this task can be solved using sort. And in general, it is better to get used to go through the array through the map and not through the loopLeo Bird
A difficult question really. I don't mean what's in the title, but what you essentially want. Do you want everything at once . But you can't want like that , alas.
To begin with, you learn the language for a few days. Suppose you have already had time to study / practice for 50 hours. An expert's level is conventionally 10,000 hours. Divide.
It turns out that your knowledge-skills are only 0.5% of the expert level.
That is, this is only the beginning of the path, and you won't be able to just take and learn in 1 question here.
Further, bubble-type algorithms and other algorithms from the textbook are classics. These algorithms are well understood, and you can't reinvent them. Well, of course, you have the opportunity to reinvent these bicycles from scratch, but you have to go a long way, not looking anywhere . But why? For what? It is better to look at the algorithms from the textbook in the textbook and not worry too much about it. In a competent training program, you can find tasks that are presented to you with increasing complexity, and usually yesterday's material can be used to solve today's problem. Would you like to learn in a non-structured, quick and easy way? No, it doesn't work that way.
But where you can take a walk is when solving your specific problems, which are not in the textbook. But here, too, not everything is so simple. Those respondents who give instant answers here already have a decent amount of experience behind them. That is, they have ready-made or almost ready-made solutions that they simply get out of their heads. They have already solved similar problems, and they understand. If you are faced with a problem that you have never dealt with, then it takes time to comprehend it and start thinking. Scientifically, it takes 23 minutes to do this. That is, you read the problem, its exact formulation, and then you start thinking - and it will take about 23 minutes before the solution begins to emerge. At this time, you can walk, look out the window, do routine things, do not interfere with the brain to work (do not upload it to YouTube and articles).
Your own solutions are the most valuable learning experience. This is growth. Some architectural solutions take several days.
Of course, this is how you need to start thinking after studying the theory, or at least simultaneously with it. So if the syntax of the language is not familiar, then you need to study it first. Loops and branches are the basis of programming. So you can spy on, but not ready-made solutions, but the language syntax itself according to documentation or See the tutorial .Levi Murillo
There is one twist to this whole situation: JS has a
sortmethod for arrays. He specifically makes sure that the sort goes from start to finish, and does not stop somewhere in the middle. You are trying to take an approach from older languages that do not have a wrapper called
sort. There are only loops, and in order to sort something, you only need to use them and another extra variable that will store a temporary value. This was a good approach 30 years ago when BASIC-86 was popular, but nowadays it only makes sense to use such things if you really invent a sort that works faster. And then, looping through the array is not the thing that helps speed up the work in this case.
// Отсортировано по возрастанию
console.log(a.sort((b, c) => b > c ? 1 : -1))
The sorting function in
sortworks like this: if you want the first element to go before the second, you need to return -1. If nothing needs to be changed - 0, if the first element should come after the second, you need to return 1. Using this simple logic, you can use any sorting strategy inside the sorting function. For example, sort the given array in ascending order on Mondays, and refuse to sort at all on Saturday and Sunday. Or, you can sort so that first the odd numbers go in ascending order, and then the even ones go in descending order.Lila Meyer
To place the code, please use CodePen or similar tool. Thanks you!