Advertisement
dimipan80

Compare Chars

Nov 10th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function compareChars(arr1, arr2) that compares two arrays of chars
  2. lexicographically (letter by letter). Write JS program charComparer.js that invokes your function
  3. with the sample input data below and prints the output at the console. */
  4.  
  5. "use strict";
  6.  
  7. function compareChars(arr1, arr2) {
  8.     if (arr1.length == arr2.length) {
  9.         for (var i = 0; i < arr1.length; i += 1) {
  10.             if (arr1[i] !== arr2[i]) {
  11.                 return 'Not Equal';
  12.             }
  13.         }
  14.         return 'Equal';
  15.     } else {
  16.         return 'Not Equal';
  17.     }
  18. }
  19.  
  20. var arr1 = ['1', 'f', '1', 's', 'g', 'j', 'f', 'u', 's', 'q'];
  21. var arr2 = ['1', 'f', '1', 's', 'g', 'j', 'f', 'u', 's', 'q'];
  22. console.log(compareChars(arr1, arr2));
  23.  
  24. arr1 = ['3', '5', 'g', 'd'];
  25. arr2 = ['5', '3', 'g', 'd'];
  26. console.log(compareChars(arr1, arr2));
  27.  
  28. arr1 = ['q', 'g', 'q', 'h', 'a', 'k', 'u', '8', '}', 'q', '.', 'h', '|', ';'];
  29. arr2 = ['6', 'f', 'w', 'q', ':', '”', 'd', '}', ']', 's', 'r'];
  30. console.log(compareChars(arr1, arr2));
  31.  
  32. arr1 = [];
  33. arr2 = [];
  34. console.log(compareChars(arr1, arr2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement