Click here to Skip to main content
15,124,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone !
In advance i want to say , that i am learning programming , and its possible that i dont understand something from basics , so my problem is that i have a for loop and this for must sort anagrams , but its not working code here->

JavaScript
var arr = ["воз", "ЗОВ", "корсет", "киборг","киборг", "гробик", "костер", "сектор"]; /*array with elements in russian */
var arr2=[]; /* new array where we are going to write sorted elements (anagrams) from arr */

/*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('')); /* it sorts every element from arr by letters */
	}
	var sarr=[]; /* this is new array , because we need to write sorted elements here , but only one which contains same letters */
	
	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]);// ARR I
	}
console.log(sarr);
}

clear(arr);
console.log(arr)

so when here is "arr[i]" it writes in sarr array every 8 element(by writing in console) , but when here is "arr2[i]" it writes here only 3 unique elements , why it dont working correctly , please tell me , i am dont understand . This will work
JavaScript
var arr = ["воз", "ЗОВ", "корсет", "киборг","киборг", "гробик", "костер", "сектор"]; /*array with elements in russian */
var arr2=[]; /* new array where we are going to write sorted elements (anagrams) from arr */

/*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('')); /* it sorts every element from arr by letters */
	}
	var sarr=[]; /* this is new array , because we need to write sorted elements here , but only one which contains same letters */
	
	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]);
	}
console.log(sarr);
}

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

i have tried to change this both of cycles but nothing works , i'm trying to solve this problem about 3-4 days . I know how to make this without this both cycles , but i want to know why its not working ,i am interested in learning , not just to make this work , if this is wrong way, why it is wrong?
Thanks in advance !!!

What I have tried:

I have tried to change this without next: but its dont working it gives me 65 elements , so this way was wrong ( sorry i cant write this code again becauuse i have writed it couple days ago.
Posted
Updated 10-Oct-17 8:27am
v5

1 solution

Well - take a look at how for loops are done in javascript. And javascript, in general, at this address. JavaScript for Loop [^]

Hint 1: your use of "next" is so very very wrong.
   
Comments
Artyom M 12-Oct-17 14:01pm
   
Thanks for your answer. I already know that my for working wrong , because i am writing at sarr the value from arr , and then i compare values from arr2 with arr , and this will not be equal , and will not work , because my if condition was written wrongly :) thank you for your attention again .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900