Home > Enterprise >  How to perform arithmetic on the individual digits of a number
How to perform arithmetic on the individual digits of a number

Time:11-30

There is a function that takes two arguments. The first argument is the number of digits in the number. The second argument is the number itself. (5, 12345). Arguments will be passed in this format). It is necessary to take the first and last digit of this number and add them. Then return the product of these new numbers.

Example solution ->

arguments(5, 12345)
(1 5)*(2 4)*3 = If the number of digits is odd
arguments(6, 123456)
(1 6)*(2 5)*(3 4) = If the number of digits is even

Here is the code that I wrote, for some reason it outputs NaN constantly, how to fix it and how to write conditions for the loop?

var a = prompt("Number: ");
a = Array.from(a).map(i => Number(i));

if (a.length % 2 == 0) {
    result = (a[0]   a[a.length - 1]) * (a[1]   a[a.length - 2]) * (a[1]   a[a.length - 2]);
    alert(result);
} else {
    result = (a[0]   a[a.length - 1]) * (a[1]   a[a.length - 2]) * a[3];
    alert(result);
}

CodePudding user response:

You need a generic version of your solution.

Try like this:

let a = prompt("Number: ");
a = Array.from(a).map((i) => Number(i));

const n = Math.floor(a.length / 2);
let answer = a.length % 2 === 1 ? a[n] : 1;

let i = 0;
while (i < n) {
  answer *= a[i]   a[a.length - (i   1)];
  i  
}

alert(answer)

CodePudding user response:

is this what you want?

var a = prompt("Number: ");
a = Array.from(a).map(i => Number(I));
var result = 1;
var k = 0;
for (j = a.length - 1; j >= a.length / 2 ; j--) {
    result *= a[j]   a[k];
    k  ;
}
if (a.length % 2 == 0) {
    alert(result);
} else {
    result *= a[k];
    alert(result);
}

CodePudding user response:

We can do this with simple while loop.

let argument = (Numberlength, num) => {

  let arr = [...(num   '')].map(Number)

  let i = 0;
  let j = Numberlength - 1; // last element or arr.length -1
  let product = 1;
  while (i <= j) {
    let sum = arr[i]   arr[j]
    if (i === j) { //  incase odd we only need to add one
      sum = arr[i];
    }
    product *= sum;
    i  ;
    j--;
  }
  return product
}
console.log(argument(1, 12345)); // 1 
console.log(argument(2, 12345)); // 1   2 
console.log(argument(3, 12345)); // (1   3) * 2
console.log(argument(4, 12345)); // (1   4) * (2   3)
console.log(argument(5, 12345)); // (1   5) * (2   4) * 3  

CodePudding user response:

Here you can try this logic : (with process of mathematical operators apply)

let str = "";

function check(len, num) {
  let numArray = Array.from(String(num), Number),
    temp = 1,
    result = 1;

  while (numArray.length != 0) {
    let first = numArray.shift();
    let last = numArray.pop();
    let checkIsAvailable = numArray[0];

    str = str   `(${first}`;

    if (last) {
      str  = ` ${last})`;
    } else {
      str  = ")";
    }

    if (checkIsAvailable) {
      str  = "*";
    }

    if (first == undefined) first = 0;
    if (last == undefined) last = 0;

    result = result * (first   last);
  }

  return result;
}

let sum = check(5, 1234567);

console.log(sum);

console.log(str);

  • Related