# 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

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

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

``````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.

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.