Practice exercise - functions and arrays #36

Hi everyone
#4 question, when I check the solution
there is a syntax error there is a extra parenthesis.

And also I need help who can explain to me
why is the result is 9 not 11 when I run the code
on the solution on question no. 4
because on the list the largest is 11 not 9, I’m just wondering and confuse, :slight_smile:
maybe someone can enlighten me how it works.
Thanks in advance :wink:

Can you show your code?

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


here is the solution.
Thank you.

Think the solution is incorrect. num is just the loop counter but line 3 in solution b and c compares it with the value of smallest and largest respectively.

if (num > largest)

Values of num can only be 0, 1, 2, 3, 4, 5, 6, 7, 8.

But we need to check what list[num] is instead and compare that with smallest and largest.

3 Likes

fmjh!

That’s correct, an error has snuck into the solution I’m glad you caught it.
:wave::raised_hands:

Ivo

2 Likes

Thanks guys,
I really appreciate it. :+1:

1 Like

Hey guys,

can someone explain this to me. I am stuck on exercise 30 months.

if (number > 0 && number <13) {
console.log (number + “-” + months [number-1])

I dont fully understand why number -1 is there. why is that needed? is it because January is actually 0? so as an example i want the console to respond January so i enter 1 meaning January but the array function would actually reply February. So with -1 it will actually take the value 0 and log January. i hope that makes sense.

1 Like

Hi Geetay,

Yes you are right.

It’s because months[0] references January, yes. Arrays are ‘indexed’ from 0 and not 1. If you are using ‘number’ to get the months from 1 to 12 then you will always need to subtract 1 from number to point to the correct month in the months array.

If you have an array of anything the first element of the array always has an index of 0 and the index of the last element is always equal to the length of the array - 1.

Cheers,

fmjh

3 Likes

Hi fmjh,

Thank you so much for clarifying. Greatly appreciated. It makes sense now.

Thanks,

Geetay

2 Likes

I had a hard time doing this exercise and didn’t come to a solution by myself…
I don’t get what this line wants us to tell:

for (num in list)

I never read that before to be honest. What exactly does this do? And why is “num” not defined? If I execute the code without defining “num” before the line above, I get an error.

3 Likes

Hey @jott, hope your ok.

Which exercise are you talking about? i don’t remember which is :frowning:.

Also if you can share your code I can help you review it :nerd_face:

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi @thecil doing great, how about you? :slight_smile:
Pardon me, here’s the code from the solution of the academy:

// a) Define a list that contains the following values: 6, 4, 1, 7, 2, 8, 3, 9, 11.

const list = [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.

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

// c) Use a new for-loop corresponding to problem b, but find and print the largest value.

var largest = list[0]; for (num in list)) {
if (num > largest) { largest = list[num];
} }
console.log(largest);

What does “(num in list)” do? I don’t get it and never saw a for-loop like this before. :no_mouth:

3 Likes

Hi @jott,

It just another syntax for a for loop.
It basically says, for each element in the array pick it up, call it num and then continue using it inside the loop.

So, for every loop, num is different.

In the example you provided,
num for the first iteration is 6
num for the second iteration is 4
num for the third iteration is 1
and so on…

The variable num is implicitly declared within the context of the for loop. That is why you don’t see it declared before.

Hope this clears it out.

Happy learning!

4 Likes

Hi guys,

I was trying to write the code for integrate an array and i get a reference error $ is not defined. I have looked at Ivan’s code and i cant see what i have done differently. Can somebody help me. Greatly appreciated :slight_smile:

var cars = [“bmw”, “volvo”, “ford”];

$.each(cars,function(index, value){
console.log("car " + value + “is at index” + index + “in the array”);
});

Hi @Geetay, have you included the jQuery file or url in ur index.html? $ is used to define jQuery.

Happy learning, :slight_smile:
Abel S

Console.log("half of 100 is ${100/2}")
VM599:1 Uncaught ReferenceError: Console is not defined
    at <anonymous>:1:1
(anonymous) @ VM599:1
Console.log("half of 100 is ${100 / 2}")
VM607:1 Uncaught ReferenceError: Console is not defined
    at <anonymous>:1:1
(anonymous) @ VM607:1
HI,
Not sure what is wrong here....anyone see my error.
Thanks in advance for the help

@monymike

Hi @monymike , console.log should be written all small letter, I see you have C cap letter.

Happy learning, :slight_smile:
Abel S

1 Like

Hey abuga,

Man, i thought i already the jQuery file i the code. Now i see that i dont :rofl: thanks man. i cant believe. i was scratching my head confused. appreciate it

1 Like

Hi @Malik & @jott thanks for helping to elaborate on practice #36!

Apologies in advance for the long entry!!

To try and better understand how the for…in loop works, I added a console.log(num) within the for loop as well.

  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(num);
  }
  console.log(smallest);

The output for console.log(num) were the indices. 0, 1, 2…8 of the array.

Questions:

  1. Does that mean that the binding num stores both the index of the array and the value? Because it must be storing a value for ‘number < smallest’ to work wouldn’t it? :thinking:

  2. Here’s where I got a little confused, so I changed the script to smallest = num, and the console.log(smallest) outputs 0. This means that num was represented as an index. And if that’s the case, how did ‘number < smallest’ work?

As I was reading up more, I realised we could use a for…of loop for this as well.

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

  var smallest = list[0];

  for (num of list){
    if (num < smallest){
      smallest = list[num];
    }
    console.log(num);
  }
  console.log(smallest);

In this case the console.log(num) output the values, 6, 4, 1…11. So then num < smallest made sense to me.

However, I realised that the script worked regardless of where I use smallest = list[num] or * smallest = num*. Which then got me confused again and it goes back to my earlier questions on top.

Thanks for reading all the way through!

– Chin