0

I am Beginner in Javascript, and created a program Adding Indeces of an Array numbers would come equal to the target given, and Printing the indeces as an output.

eg - Input is nums array= [1,2,3,4,5] and target = 9 adding 3th & 4th element would give the target value (5+4=9), so output would be 3,4

If i pass the values from Prompt, the code is not working. However if i pass the values directly, the code is working fine. I do not know why is this happening, is there any concept, that i am lacking. Please give your suggestions :)

Code is Below:

// taking array from user
let nums = [];
let size = 5; // Array size 

for (let a = 0; a < size; a++) {
    nums[a] = prompt('Enter array Element ' + (a + 1));
}

// taking Sum Target from user
let sum = prompt("Enter the Sum Target");

for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
        if (nums[i] + nums[j] === sum) {
            alert(`The Two indices are ${i} and ${j}`);
        }
    }
}
  • Does this answer your question? [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – lusc Jun 08 '22 at 09:57
  • `nums[a] = +prompt(....)` and `sum = +prompt(....)` – adiga Jun 08 '22 at 10:01

0 Answers0