CodeWars | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

CodeWars

Idk if it’s ok to ask questions about codeWars But when I submit my answer i get reference error does this mean that I have an error in my code or what because I think my code is right because i tried it on an interpreter and i got the result i am asked to

21st Aug 2019, 12:36 PM
Majd Alkwaja
Majd Alkwaja - avatar
10 Answers
+ 3
I believe it's because you didn't actually make a string. It's easily correctable, though: var res = ""; // ... res += "("; // ... res += ")"; // ... return res;
21st Aug 2019, 12:57 PM
Airree
Airree - avatar
+ 2
Based on how Codewars work, I think you should be returning the resulting string from your function as an answer, instead of using document.write() to write it on a HTML document. You should be using a return statement. function duplicateEncode(word) { let newStr = ''; // build a new string based on word return newStr; }
21st Aug 2019, 12:55 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Use toUpperCase or toLowerCase methods to ignore capitalization.
21st Aug 2019, 1:22 PM
Calviղ
Calviղ - avatar
+ 2
You have to make below function return all the correct results function duplicateEncode(word){ // ... } Which at least pass below tests: Test.assertEquals(duplicateEncode("din"),"((("); Test.assertEquals(duplicateEncode("recede"),"()()()"); Test.assertEquals(duplicateEncode("Success"),")())())","should ignore case"); Test.assertEquals(duplicateEncode("(( @"),"))((");
21st Aug 2019, 1:35 PM
Calviղ
Calviղ - avatar
+ 1
Here's the question: "The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate." My answer is: function duplicateEncode(word){ for(var i=0; i < word.length; i++){ if(word.indexOf(word[i]) == word.lastIndexOf(word[i])){ document.write('('); } else if(word.indexOf(word[i]) !== word.lastIndexOf(word[i])){ document.write(')'); } } }
21st Aug 2019, 12:49 PM
Majd Alkwaja
Majd Alkwaja - avatar
+ 1
Could You Tell Me If This Is The Right Way To Ignore The Cases: function duplicateEncode(word){ var res = ''; word.toLowerCase(); for(var i=0; i < word.length; i++){ if(word.indexOf(word[i]) == word.lastIndexOf(word[i])){ res += '('; } else if(word.indexOf(word[i]) !== word.lastIndexOf(word[i])){ res += ")"; } } return res; }
21st Aug 2019, 1:18 PM
Majd Alkwaja
Majd Alkwaja - avatar
+ 1
toLowerCase method is not modify origin string, it return a new string.
21st Aug 2019, 1:54 PM
Calviղ
Calviղ - avatar
0
Thanks i did it ❤️❤️❤️
21st Aug 2019, 1:57 PM
Majd Alkwaja
Majd Alkwaja - avatar
0
function duplicateEncode(word){ var res = ''; var w = word.toLowerCase(); for(var i=0; i < w.length; i++){ if(w.indexOf(w[i])===word.lastIndexOf(w[i])) { res += '('; } else { res += ")"; } } return res; } https://code.sololearn.com/W4nA65G9l5R4/?ref=app
21st Aug 2019, 2:02 PM
Calviղ
Calviղ - avatar
0
but,is there a way to specify the level of questiones on codeWars
21st Aug 2019, 2:03 PM
Majd Alkwaja
Majd Alkwaja - avatar