Wrong solution: Practice exercise - Functions & Arrays No.36

Hi,

I found out there is a wrong solution on exercise 36. b)

Task:
a) Define a list that contains the following values: 6, 4 , 1 , 7 , 2 , 8 , 3 , 9 , 11.
b) Use a for-loop to iterate through all the values in the list and find the smallest
value. Print the smallest value. Do this without using JavaScript’s built-in min
function.

Solution in PDF:

const list = [6, 4, 1, 7, 2, 8, 3, 9, 11];
var smallest = list[0];
for (num in list) {
    if (num < smallest) {
      smallest = list[num];
    }
}
console.log(smallest);

Now if you run this it gives you result 1. Which is the smallest and therefore correct.

However something seemed wrong to me there. So I tried to understand the code and I tested it for different array using the same logic:

const list = [6, 4, 12];
var smallest = list[0];
for (num in list) {
    if (num < smallest) {
      smallest = list[num];
    }
}
console.log(smallest);

And this will give you result 12. Which is not smallest and is wrong.

The logic above will give you the correct result only if there is a number in array which is smaller than array.length. It simply does not search for the smallest value.

Am I right? Please look at this. Thanks.

2 Likes

This is my solution (please consider I am beginner :smiley: )

const list = [6, 4, 1, 7, 2, 8, 3, 9, 11];
var result = 99999999999999999999999;
for (i in list) {
    if (list[i] < result)
    var result = list[i];
};
console.log(result);

This should work most of the times. If we want a solution which is more complex but works everytime no matter array we have, then I came out with this (I replaced 99999999999999999999999 with function):

const list = [6, 4, 1, 7, 2, 8, 3, 9, 11];
var maxNumber = list.reduce(function(a, b) {
    return Math.max(a, b);
    }
);
var result = maxNumber + 1;
for (i in list) {
    if (list[i] < result)
    var result = list[i];
};
console.log(result);
1 Like

Will get back to you on this one. We are cross-checking this solution.

1 Like

hi @Tomahawk,
You pointed it out correctly.

We will change the solution from our side. Thanks for catching this.

The correct solution ought to be –

var list = [10,11,50];
var smallest = list[0];

for(n of list){
 if(n < smallest){
  smallest = n;
 }
}
console.log(smallest)
2 Likes

Usually, the for..in syntax is not used for arrays or iterable things. It is mostly used for iterating object properties. so, refrain from using for..in for arrays.

For…of -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

For…in -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

Links for reference and study.

Hope this helps. :slight_smile:

1 Like

Happy to contribute :partying_face:

Yep this is more simple and more clear solution than mine.

1 Like

Thanks for improving the content quality in our academy… :clap: :clap:

1 Like

I tried to educate on for … in and for … of. And I tried it on Exercise 37.

So there are possible two solutions.

First one you have in PDF uses for…in:

var courses = ["Blockchain & Bitcoin 101", "Ethereum 101", "JavaScript for Blockchain Developers", "12-Week Bootcamp", "Bitcoin Programming 101"];

for (n in courses) {
	console.log(courses[n]);
};

Am I understanding this correctly if I say that “n” stands here for the index? Given expression console.log(courses[n])?

My second solution using for…of: (which should be more right since it is array?)

var courses = ["Blockchain & Bitcoin 101", "Ethereum 101", "JavaScript for Blockchain Developers", "12-Week Bootcamp", "Bitcoin Programming 101"];

for (n of courses) {
     console.log(n);
};

And in this case “n” stands for the actual string/value in array becasue of expression console.log(n)?

And this should be the difference between the two? For…in uses index and for…of uses string/value?

so for the greatest we just change < to >.
also I confused why the var smalles = list[0]
what does list[0] mean. I thought list[0] = 10

list[0] is indeed 10.
There is var smallest = list[0] because you have to set some value from the array first and then you compare numbers from the array to this value. And when something is smaller than this value, then the value is updated by this number. At the end var smallest is the smallest number from the array.

I’ve read through this thread a couple times already ,but I’m still having issues with this exercise. I keep trying to input the solution into the chrome console and playcode.io ,but I keep getting an error.

Here is a screenshot of chrome console error.
image

Chrome console says this is the problem line.
image

Here is a screenshot from playcode.io
image

If someone can walk me through this code or tell me what I’m doing wrong it would be greatly appreciated. I just want to understand how it works.

Hi @Renz,

row 14 should be this way:

for (n of arr) {

You had an additional “)” there. Also the error tells you that: "Uncaught SyntaxError: Unexpected token ‘)’ "

It means that you had mistake in brackets.

2 Likes

Thank you, @Tomahawk.

I removed the extra “)” ,but now I’m getting two different errors.

Playcode error
image

Chrome console error
image

I don’t understand what is going on here. :frowning:

@Renz

  1. Uncaught ReferenceError: n is not defined
    You didn’t define n. First you need to declare n, for example on row 6 this way:

for(let n of arr) {

  1. "Identifier ‘list’ has already been declared
    On row 2 you defined list as const list, this means it is constant and can’t be overwritten once you declare it. When you run your code for the first time in console, you won’t get this error. However when you run it second time you will get error, because you want to define const list again. Which is not possible.

This means you need to open new window and run console again, or for test purposes you declare list as var, not as const.

var list = [6,4,1,7,2,8,3,9,11];

2 Likes

Hey @Tomahawk, thank you for the detailed explanation. I removed the extra “)” and changed const to var and it made the code work. Thank you for your help, MUCH appreciated! :smiley:

Chrome Console
image

playcode
image

1 Like