0

How do I apply my constant array by an array the user inputs and get the sum?

The user always enters 6 digits but constant is 5 digits

Example

[5, 7, 3, 5, 2] is my constant array that I am multiplying by

user enters 837465

turns into [8, 3,7,4,6,5] (but I want 5 to be ignored)

multiply by constant array(not including last element) and get sum:

(8*5)+ (3*7) + (7*3) + (4*5)+ (6*2) = 114

const arr = [5, 7, 3, 5, 2];
var arr2 = [];
var num = parseInt(document.getElementById("yourNumber").value); //input from .html

var sum = 0;
for (var i = 1; i< num.length; i++){  //var i = 1 b/c user always enters 6 digits, i feel this is wrong?
    arr2.push(parseInt(num[i]));
    sum += (arr2[i]*arr[i]);
}

console.log(sum);
2
  • i should go from 0 to arr.length. Make sure to check if your number has enough digits before the loop (>=arr.length). Also, you don't need to convert the value to integer, since you will be using each digit (i.e, each character) separately. Then, the operation is just sum+=arr[i]*num[i], you can access each character of a string as if it were an array. Commented Apr 2, 2020 at 23:29
  • thank you!!! it worked Commented Apr 2, 2020 at 23:37

4 Answers 4

1

try use const input = '837465'.split('') or ready array const input = [8, 3, 7, 4, 6, 5] and reduce() method for summation array. reduce will work while there are elements in the original array

const array = [5, 7, 3, 5, 2]
const input = '837465'.split('')
//or 
// const input = [8, 3, 7, 4, 6, 5]

const summ = (input) => array.reduce((acum, rec, index) => acum + (rec * input[index]), 0)

console.log(summ(input))

Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful answer !
0

Following works:

const arr = [5, 7, 3, 5, 2];
var num = document.getElementById("yourNumber").value; //input from .html

var sum = 0;
for (var i = 0; i< num.length-1; i++){  //var i = 1 b/c user always enters 6 
digits, i feel this is wrong?
    sum += (arr[i]*parseInt(num[i]));
}

console.log(sum);

Comments

0

You can do it imperatively using the legible for-of syntax -

const arr = [5, 7, 3, 5, 2]
const nums = [8, 3, 7, 4, 6, 5]

let result = 0

for (const [ key, value ] of Object.entries(arr))
  result = result + (value * nums[key])

console.log(result) // 114

Or you can do it declaratively using Array.prototype.reduce -

const arr = [5, 7, 3, 5, 2]
const nums = [8, 3, 7, 4, 6, 5]

const result =
  arr.reduce
    ( (sum, value, key) => sum + (value * nums[key])
    , 0
    )

console.log(result) // 114

Comments

0

Here is the answer (please click on Run code Snippet)

If you need any explanation, or this is not what you are asking about, please comment on my answer

function getTotal() {
  const arr = [5, 7, 3, 5, 2];
  var userNumbers = document.getElementById('yourNumber').value.split(''); 
  var sum = 0;
  //itreate over your array case it is short (or a condition to itreate over the shortest one)
  for (var i = 0; i < arr.length; i++) {
    sum += parseInt(userNumbers[i]) * arr[i];
  }

  console.log(sum);
  document.getElementById('answer').innerHTML ='Answer will be here: '+ sum;
}
<html>

<head>
  <title>StackOverFlow &#127831;</title>
  <link rel="shortcut icon" type="image/x-icon" href="https://image.flaticon.com/icons/png/512/2057/2057586.png" />
</head>

<body>
  <h3>Stack Over Flow &#127831;</h3>

  <input id="yourNumber" type="text" value="837465">

  <button onclick="getTotal()">get the answer</button>
  
  <p id="answer">Answer will be here: </p>

</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.