Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How do I multiply even and uneven numbers inside an array using an loop

Hello everyone,

First of all; I have been coding since February now so as you can understand im a real beginner.

* For a school assignment I have to use a loop to multiply all the even and uneven numbers inside an array.
* The script must work even if the array contains a trillion numbers.

Down here you can see the script that I have written so far.
My small knowledge mind tells me that the script is complete and correct apart from the '///' lines.
However, nothing could be less true and maybe the problem lies somewhere else.
I have tried multiple ways since yesterday to add the multipliers into the '////' lines but nothing seems to work 🙁

Really, I don't want to get emotional or anything out here right but honestly my brain hurts. Can someone release me of this pain ?

Code:
const numbers = [2, 4, 5, 29, 38];
for (let i = 0; i < numbers.length; i++) {
    if (i % 2 === 0) {
////---->MULTIPLY X2<------
    } if ( i % 2 !== 0) {
////---->MULTIPLY X3<------
    }
}
console.log(numbers)



This is one of my best tries.

Code:
const numbers = [2, 4, 5, 29, 38];
for (let i = 0; i < numbers.length; i++) {
    if (i % 2 === 0) {
console.log(numbers[i] * 2)
    } if ( i % 2 !== 0) {
        console.log(numbers[i] * 3)
    }
}
console.log(numbers)

However; to complete the assignment log must say;
4, 8, 15, 87, 76 ];

My log after my best try says:
4 12 10 87 76

12 must be 8
10 must be 15
 
Last edited:
Solution
D
Is the assignment actually clear to you ? The statement "multiply all the even and uneven numbers inside an array" seems equivalent to "multiply all the numbers in the array" - that could be one hell of a big number, especially when there's a trillion numbers in the array. I don't understand that second requirement anyway - either your loop works or it doesn't, regardless of the array size. And are you required to actually change the array or just print some output like you are doing ? Your final console.log(numbers) seems to suggest you do want to change them

Your loop is trying to multiplying the numbers by 2 or 3, depending whether they are odd or even. If the result for [2, 4, 5, 29, 38] is expected to be...
Is the assignment actually clear to you ? The statement "multiply all the even and uneven numbers inside an array" seems equivalent to "multiply all the numbers in the array" - that could be one hell of a big number, especially when there's a trillion numbers in the array. I don't understand that second requirement anyway - either your loop works or it doesn't, regardless of the array size. And are you required to actually change the array or just print some output like you are doing ? Your final console.log(numbers) seems to suggest you do want to change them

Your loop is trying to multiplying the numbers by 2 or 3, depending whether they are odd or even. If the result for [2, 4, 5, 29, 38] is expected to be 4, 8, 15, 87, 76 then this seems to be what you need to do (despite the poorly stated assignment - good job they give you the answer). So why are you getting the wrong output ? Look again, you are testing the index if (i % 2 === 0) rather than the number at that index if (numbers[i] % 2 === 0)

So let's rewrite that :

JavaScript:
const numbers = [2, 4, 5, 29, 38];
for ( let i = 0; i < numbers.length; i++ )
{
    if ( numbers[i] % 2 == 0)
    {
        numbers[i] *= 2
    }
    else
    {
        numbers[i] *= 3
    }
}
console.log(numbers)

Things you may notice:
1) We now test the number instead of the index
2) We use == instead of ===. There is no need to compare datatypes here, so no need to use ===
3) Using if-else instead of two if statements. The number is either even or odd.
4) Using *=, the multiply AND assignment operator
5) The array elements are is now being modified
 
Solution
Is the assignment actually clear to you ? The statement "multiply all the even and uneven numbers inside an array" seems equivalent to "multiply all the numbers in the array" - that could be one hell of a big number, especially when there's a trillion numbers in the array. I don't understand that second requirement anyway - either your loop works or it doesn't, regardless of the array size. And are you required to actually change the array or just print some output like you are doing ? Your final console.log(numbers) seems to suggest you do want to change them

Your loop is trying to multiplying the numbers by 2 or 3, depending whether they are odd or even. If the result for [2, 4, 5, 29, 38] is expected to be 4, 8, 15, 87, 76 then this seems to be what you need to do (despite the poorly stated assignment - good job they give you the answer). So why are you getting the wrong output ? Look again, you are testing the index if (i % 2 === 0) rather than the number at that index if (numbers[i] % 2 === 0)

So let's rewrite that :

JavaScript:
const numbers = [2, 4, 5, 29, 38];
for ( let i = 0; i < numbers.length; i++ )
{
    if ( numbers[i] % 2 == 0)
    {
        numbers[i] *= 2
    }
    else
    {
        numbers[i] *= 3
    }
}
console.log(numbers)

Things you may notice:
1) We now test the number instead of the index
2) We use == instead of ===. There is no need to compare datatypes here, so no need to use ===
3) Using if-else instead of two if statements. The number is either even or odd.
4) Using *=, the multiply AND assignment operator
5) The array elements are is now being modified
Thank you very much.
1)If I want to test something inside an array I always test the const instead of just the index.
4)I did not know about the multiply AND assignment. It was hard for me to place the * because webstorm kept telling me it needs an assignment.

Thank you
 
1)If I want to test something inside an array I always test the const instead of just the index.
Not sure what you mean with "the const". I would say test the array element, not the index.
4)I did not know about the multiply AND assignment. It was hard for me to place the * because webstorm kept telling me it needs an assignment.
Not sure what your issue with webstorm is regarding the *.

What does amaze me is that JavaScript lets you modify the elements of an array that is defined as const. That is rather counter-intuitive I think... Unless someone can explain ?
 
Not sure what you mean with "the const". I would say test the array element, not the index.

Not sure what your issue with webstorm is regarding the *.

What does amaze me is that JavaScript lets you modify the elements of an array that is defined as const. That is rather counter-intuitive I think... Unless someone can explain ?
Yes ! I can explain; sorry for my late reply.

Basically it is being 'overwritten' and not 'modified'.

So you can not add completely new array/object but you CAN overwrite existing values.
 
Basically it is being 'overwritten' and not 'modified'.

So you can not add completely new array/object but you CAN overwrite existing values.
There must be a very fine line between overwritten and modified... I for one don't see it 😀
But yes, it seems like in a const array you can modify all the elements, just not add or delete elements. So much for the concept of being constant ! ? Ah well, there is probably a good reason for this being so.

But more importantly, is your problem solved now ?
 
There must be a very fine line between overwritten and modified... I for one don't see it 😀
But yes, it seems like in a const array you can modify all the elements, just not add or delete elements. So much for the concept of being constant ! ? Ah well, there is probably a good reason for this being so.

But more importantly, is your problem solved now ?
Yes, that's exactly what I mean by 'overwriting'.

Yes the the problem is solved, thank you !
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom