Why does it display nan?

  function multiply(arr, n) {
    let product = 1;
    for (let i = 0; i < n; i++) {
      product *= arr[i];
    }
    return product;
  }
console.log(product)
multiply(3,6)

if app=3 and n=[0,1,2,3,4,5] then how can I multiply by the product?

You’ve got a couple issues here…

  • 3 is not an array. [3] is an array in javascript terms.

  • 6 will fail if you pass a less than six element array.

  • multiply(3,6) returns NaN because 3 is not an array

  • multiply([3],6) returns NaN because there is only one element to the array, not 6

  • multiply([3],1) returns 3 because 1 * 3 is 3.

For your “if” question (which is backwards, btw)

  • multiply([0,1,2,3,4,5], 3) returns 0 because 1 * 0 * 1 * 2 is 0.

but

  • multiply([1,2,3,4,5,6], 3) returns 6 because 1 * 1 * 2 * 3 is 6.
  • multiply([1,2,3,4,5,6], 6) returns 720 because 1 * 1 * 2 * 3 * 4 * 5 * 6 is 720.

How complicated everything is here, ))), this language is impossible to understand)))

Only because you’ve decided it’s impossible to understand. If you take the time to work through line by line what each thing is doing, it’s not as difficult as you’re making it out to be…

2 Likes

I have been studying this language for almost 2 years, but the result for me was zero; I never understood Javascript logic)

So why don’t you look for another hobby? To be honest, but if you still not understand the absolutely basics after two years, maybe your talent in mathematical and structures thinking is not good enough to become a developer….

I would not try to become a teacher, as my talent in having patience is very poor :slight_smile:

This is not my hobby, I am a successful girl, I have everything, I do this just like gymnastics, I have time to afford it, but I have an even larger audience of people where I discuss different topics, but I don’t have haven’t seen more than one person, smart, who ideally knows how to introduce or explain Javascript, Python, etc. That’s why people need to explain it so that they don’t waste time learning such languages,

That’s why I approach these languages differently and don’t see anything good in it.

So why do you keep persuing javascript if you don’t understand it, don’t need it, and don’t seem to like it?

3 Likes

Because many people told me that a Java script is impossible, to understand how it works, I, of course, did not believe it, to believe it, you need to check it, I was raised that way, now I am convinced of it. and I can say with confidence that this is a waste of time. no one can explain how the codes work

Let’s clarify that a bit. They can’t explain the code in a manner that you like. I have seen MANY examples of people taking a lot of time to explain code to you line by line, and doing an excellent job of it.

3 Likes

You are talking about looping through an array to find the product using a for loop. This is hardly unique to Javascript.

Because many people told me that a Java script is impossible, to understand how it works

A quick google.

According to SlashData, JavaScript, which includes CoffeeScript and Microsoft’s TypeScript in the survey, is by far the most popular language, with 12.4 million developers using it worldwide.

1 Like

You’ve said that many times already. So again I ask, if that is your conclusion, why keep persuing it? What’s in it for you?

1 Like

I dislike Javascript too. But there is no alternatives AFAIK. So I have to deal with this beast. Hence I asked Bard to “rewrite” the code to be more readable. Do not blame me for the code below. I did not understand the initial code either :slight_smile:

function multiply(arr) {
  let product = 1;
  for (const num of arr) {
    product *= num;
  }
  return product;
}

const arr = [3, 4, 5, 6];
const product = multiply(arr);
console.log(product); // Output: 360

… or even shorter

const product = [3, 4, 5, 6].reduce((product, num) => {
  product *= num;
  return product;
}, 1);
console.log(product); // Output: 360

Basically you send an array as a parameter and iterate and multiply each value in the array (3x4x5x6=360) and finally return a value to the console.

I avoid iterating using i++ as it is not readable for me. For…of is easier to understand.

3 Likes

I think if you know how to explain, then yes, everything can be explained to a person if he knows how to write and read.

Did you ask it to change the functionality too? It’s obliterated the second parameter…

(and no, i’m not wading into the cesspit that is this eternal doomsaying about ‘its impossible’. Been there, had that conversation with you, twice at least, OP.)

Unless there’s some reason why you’d want to only multiply part of the array, that second parameter is superfluous, and/or troublesome unless you add an out of bounds check to it.

But if you wanted to keep that second parameter, it needs a check on it…

  function multiply(arr, n) {
    let product = 1;
    const loopCount = n > arr.length ? arr.length : n; // if n is bigger than the array size, just use the array size.
    // loop through the array, multiplying it times the previous element product
    for (let i = 0; i < loopCount; i++) {
      product *= arr[i];
    }
    return product;
  }
console.log(multiply([3,6], 3)); // array only has 2 elements, so multiply 1 * 3 * 6 (all elements)
console.log(multiply([3,6], 1)); // multiply 1 * 3 (just the first element...)

I didnt create the original, so I can’t vouch for the use case. All i’m saying is be careful of asking AI’s to rewrite code for you, because they won’t always get it right

2 Likes

Oh, these statistics, but if there was a result for people, it would be absolutely ideal.

I saw this also. And I asked Bard about it.

The n parameter has been removed from the code because it is now no longer needed. The for...in loop iterates over the entire array, regardless of the length of the array.

I am aware of this. But the code work and it seems to be logic. And I did not understand the original code (faulty?) but the new code seems to be better. As I pointed out, Javascript is not my closest friend :slight_smile: