Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Why does my program not return the correct math operator summation?

I have a simple calculator program below that's supposed to push the user numbers into an array and then make a calculation based off the operator they have chosen. The calculation is not working as it should and I cannot work out why.

For example, if I add 3 numbers (2,4,5), the returned answer = 245 rather than 11.

const readline = require(`readline-sync`);

console.log('Welcome to the calculator!');
console.log('==========================');

console.log('Please enter the operator:');
const operator = readline.prompt();

console.log(`How many numbers do you want to: ${operator}?`);
const howManyNums = parseInt(readline.prompt());

let numbers = Array(howManyNums);
for (let i = 0; i < numbers.length; i++) {
  console.log(`Please enter number: ${i + 1}:`);
  const argument = readline.prompt();
  numbers[i] = argument;
};

let answer = numbers[0];
for (let i = 1; i < numbers.length; i++) {
  switch(operator) {
    case '+':
      answer += numbers[i];
      break;
    case '-':
      answer -= numbers[i];
      break;
    case 'x':
      answer *= numbers[i];
      break;
    case '/':
      answer /= numbers[i];
      break;
    default:
      "That is not a valid operator";
  };
};

console.log(`Your answer is: ${answer}`);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!