Given the task to find common characters of two lines. I would be able to decide if I myself had to enter values ​​into the array. However, the user must enter. And now I do not understand how to do it. When I check the character under the index 1, 2, 3, etc. - displays undefined. Please help with the solution! Cited a simple example below.

var b = 1352254; var c = 1032; var a = [b]; var v = [c]; alert(a[3]); 
  • you don't need an array.toString (), length and charAt (n) - nick_n_a
  • It is necessary to use split () to divide the line by letter, then in a peasant way: You take a double cycle in the first run through the entire first array, in the second cycle you compare it with all elements of the second array. - Oma
  • @nick_n_a thank you very much! - Mark_8
  • @Oma good, I'll try) - Mark_8
  • I can’t insert it in response, captcha is not loaded. fiddle code here - slippyk

2 answers 2

 function IntersecArrays(A, B) { var m = A.length, n = B.length, c = 0, C = []; for (var i = 0; i < m; i++) { var j = 0, k = 0; while (B[j] !== A[ i ] && j < n) j++; while (C[k] !== A[ i ] && k < c) k++; if (j != n && k == c) C[c++] = A[ i ]; } return C; } function GetArrays() { var arr1 = (document.getElementById("str1").value).split(''); var arr2 = (document.getElementById("str2").value).split(''); document.getElementById("common").innerText = IntersecArrays(arr1, arr2); } 
 <input type="text" value="" id="str1"> <br /> <br /> <input type="text" value="" id="str2"> <br /> <br /> <button onclick="GetArrays()">Общее</button><label id="common"></label> 

    Here is an example of a program, but it displays an index.

     var a = [1,2,3,4], b = [6,7,6,4]; for(var i = 0;i < a.length;i++){ if(a[i] == b[i]){ console.log(i); } }