# Basic algorithm scripting is hard programming

the solutions for basic algorithm scripting are hard to understand, can someone provide some sources to understand the solutions correctly.

I think we would need to know which solution you are talking about.

finding largest number and return largest numbers in arrays

edit : find the longest word in a string and return largest numbers in arrays

It would be better if you provided links to these so we are certain which ones they are. I only see one exercise in basic algorithms that is about largest numbers:

Return Largest Numbers in ArraysPassed

Is this the one you are talking about? Were you able to complete it? If you are stuck on the challenge please post your code here and we can help you.

yes the largest number in array passed and longest word in string ,
i dont know how to solve then i saw the solution in forum but i still dont understand the solution too

It would be better if you asked specific questions about what you don’t understand about this challenge. I’ll get you started. The function receives an array of four items, each item also being an array of four items (numbers in this case). Can you describe to me what you need to do with the array that is passed into the function?

first is to create a nested for loop and an if statement to check the largest number, that’s my idea and i saw the same idea in the solution, but i did not understant the commented lines in this code

``````function largestOfFour(arr) {
let results = [];
for (let i = 0; i < arr.length; i++) {
let largestNumber = arr[i];
for (let j = 1; j < arr[i].length; j++) {  // why j = 1
if (arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
}
}
results[i] = largestNumber;  // i did not get this too
}
return results;
}
``````

Can you explain to me what this code is doing? Specifically, what is the variable `largestNumber` used for? What is the `for` loop doing? If you understand these two things I think you’ll answer your question about why `j` starts at 1.

`largestNumber` variable stores the first `for` loop value(that is `i`) , and first for loop iterates through the entire array and second `for` loop iterates through the nested arrays and this time the `largestNumber` variable is storing the nested arrays value(that is `j`)
that is what i understood , please tell me if i was correct or wrong?

It is much, much easier for us to help you to write your own code for a problem. Helping you understand another person’s solution is a very different skill than helping you write code yourself.

2 Likes

This is not quite correct. Let’s rewrite the code a little to make things a little easier to understand:

``````  for (let i = 0; i < arr.length; i++) {
let subArray = arr[i];
let largestNumber = subArray;
for (let j = 1; j < subArray.length; j++) {  // why j = 1
if (subArray[j] > largestNumber) {
largestNumber = subArray[j];
}
}
results[i] = largestNumber;  // i did not get this too
}
``````

Now it is clear that we are checking each sub array in the array passed into the function. Do you understand what `largestNumber` is storing now?

so largest number is storing the nested array !

`let largestNumber = subArray;`

I’m assuming you know how to access a particular item from an array. What item does `` access?

i do not know what is `` doing , i think the `j` item ( nested array)

If you don’t understand what `` is doing then I might suggest you revisit the basic JS course again, specifically Access Array Data with Indexes. The algorithm course is going to be very tough if you don’t have the basics down.

`let largestNumber = subArray;`

`` is getting the value of the first element in the sub array.

1 Like

i know that, but i am not understanding the operation with nested arrays

Sorry, I guess I didn’t understand your previous post because you said you didn’t know what `` is doing.

Can you be more specific about what you don’t understand? The function is passed in an array (via the `arr` parameter) and each element in that array is itself an array of numbers. The first for loop goes through each item in `arr`. Do you understand everything up to this point?

Each item in `arr` is an array as well. If we are using the first for loop to iterate through `arr` what do you think we are using the second for loop for? Before we start that second for loop we save off the first value in the sub array to the variable `largestNumber`. Why do you think we are doing that? (The name of the variable ought to give this away.) Inside the second for loop we are making a simple comparison and updating `largestNumber` if the comparison is true. Do you understand what this comparison is doing and why we might update `largestNumber`? After the second for loop is done we set `results[i]` to `largestNumber`. The variable `i` refers to the counter on the first for loop. Do you know why we are doing this? Remember, the goal of the function is to return an array consisting of the largest number from each provided sub-array.

I’ve asked all the pertinent questions here, now it is up to you to provide the answers.

thank you now i understood by reading line by line and what the code in every line is doing .
now I’ll explain
first we create an empty array(`results`) to store the final result,
then we create a first `for` loop to iterate through main array , but we creating 2nd `for` loop to compare for the largest number , so we initiating with the first element of the `sub array` in `largestNumber`.
Then we create second for loop to iterate through sub array to do comparison between `numbers` in `sub array` and if true the `largestNumber` will be updated.
After the comparison of the first sub array , the empty array that we created at the beginning will be updated with the number in `largestNumber`.
This process repeats until `i=arr.length`.
And finally the array with the largest numbers will be returned.

``````
[]          // empty array
4             // [i]
5             // comparing
[ 5 ]                   // result updated

13           // [i]
27           // comparing
[ 5, 27 ]              // result updated

32          // [i]
35          // comparing
37          // comparing
39          // comparing
[ 5, 27, 39 ]               // result updated

1000       // [i]
1001       // comparing
[ 5, 27, 39, 1001 ]              // result updated
``````

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.