Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:15:55+00:00 2026-05-26T13:15:55+00:00

// Write a program to sum all the odd elements of an array a

  • 0
 // Write a program to sum all the odd elements of an array

    a = Number(prompt("a:"));
    b = Number(prompt("b:"));
    c = Number(prompt("c:"));

    sum=Number(0);

    var test = [a,b,c];

    for (i=0; i<test.length;i++) {
        // All even numbers can be divided by 2
        if ((test[i]%2)>0) {
            alert(test[i] + ":odd");
        } else {
            alert(test[i] + ":even");
        }
        test[i]=0;
   }

   sum=0+test[i];
   alert(sum);

My program works brilliant up until its meant to add up all the numbers where it returns a NaN message! Any ideas on how I can sort this problem out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:15:56+00:00Added an answer on May 26, 2026 at 1:15 pm

    I see three problems with your code. (Not counting the weird formatting of the curly brackets.)

    1. Edit: looks like the question has been updated a couple of times since I started my answer.In the “if odd” test you are setting the number to zero with Perhaps you meant to zero out the even numbers so that you could later add up all the numbers and just get the odd ones, but test[i]=0 is not part of the if/else structure. It looks like it should be because of the indenting, but JavaScript ignores extra white-space.

    2. You are not adding the numbers to the sum within your loop.

    3. You are getting NaN because of this line:

      sum=0+test[i];
      

    That statement is outside the end of the loop, so at that point i is equal to the length of the array and test[i] is undefined.

    Try the following pseudo-code:

    sum = 0
    for (loop through the array) {
       if current number is odd
          sum = sum + current number
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write program calculating average of given numbers stored in an array.
Write a program that reads numbers until you read the number 999 then the
Suppose I write a program using immutable data structures in Java. Even though it
I want to write a program for finding out the sum of first and
I write simple C++ code that compute array reduction sum, but with OpenMP reduction
I have to write program that gets a number n from the user, and
Program calculates sum of numbers from 1 to N.. Child process calculates sum of
I have to write a program in which main calls other functions that test
I'm willing to write program that after 5 sec shows text that was hidden
Write a program to determine whether a computer is big-endian or little-endian. bool endianness()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.