A
A
Art2017-10-10 18:45:16
JavaScript
Art, 2017-10-10 18:45:16

I do not understand why for does not work correctly?

Hello !
I wrote a loop that should sort an array with anagrams and leave only unique elements, but the problem is that when I put it to write to another array (sarr), it writes one value, and if it doesn’t from the other, in this case it will display only 3 values as it should be

var arr = ["воз", "ЗОВ", "корсет", "киборг","киборг", "гробик", "костер", "сектор"]; 
var arr2=[]; 

/*function sort(a,b){
  return a-b;
}*/
function clear (arr){
  var arr3=[];
for(var i = 0 ; i<arr.length ; i++){
arr3.push(arr[i].toLowerCase().split(''));
arr3[i].sort();
arr2.push(arr3[i].join('')); 
  }
  var sarr=[]; /* сюда должны записываться уникальные элементы , уже отсортированные */
  
  next: 
  for(var i = 0 ; i<arr.length; i++){
    
    for(var j = 0 ;j<sarr.length ; j++ ){
      if(arr2[i]==sarr[j]) 
        continue next;
      
    }
    sarr.push(arr2[i]); /*если здесь будет записано данное значение то он выведет 3 элемента но отсортированных в алфавитном порядке */
  }
console.log(sarr);
}

clear(arr);
console.log(arr);

["взо", "екорст", "бгикор"]
but this one is not
var arr = ["воз", "ЗОВ", "корсет", "киборг","киборг", "гробик", "костер", "сектор"]; 
var arr2=[]; 

/*function sort(a,b){
  return a-b;
}*/
function clear (arr){
  var arr3=[];
for(var i = 0 ; i<arr.length ; i++){
arr3.push(arr[i].toLowerCase().split(''));
arr3[i].sort();
arr2.push(arr3[i].join('')); 
  }
  var sarr=[]; /* сюда должны записываться уникальные элементы , уже отсортированные */
  
  next: 
  for(var i = 0 ; i<arr.length; i++){
    
    for(var j = 0 ;j<sarr.length ; j++ ){
      if(arr2[i]==sarr[j]) 
        continue next;
      
    }
    sarr.push(arr[i]); /*если здесь будет записано данное значение то он выведет все 8 элементов несмотря на continur next*/
  }
console.log(sarr);
}

clear(arr);
console.log(arr);

["воз", "ЗОВ", "корсет", "киборг", "киборг", "гробик", "костер", "сектор"]

it seems that when continue next is executed, the loop should in principle not go there, but this does not happen. Please explain why it happens this way (I wonder why and not just the solution), and direct me to the right path, I can solve the problem with a different method, but I'm specifically interested in why it happens this way, because I'm learning and I want to understand so that I don't stumble upon something similar in the future .
Thanks in advance !!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gleendo, 2017-10-10
@Irtin

const arr = ["воз", "киборг", "корсет", "ЗОВ", "гробик", "костер", "сектор"];
const map = new Map();
const result = [];

arr.forEach(item => map.set(item.toLowerCase().split("").sort().join(""), item));

for (let item of map.values()) result.push(item);

console.log(result); // ["ЗОВ", "гробик", "сектор"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question