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
10 odpowiedzi
+ 3
I believe it's because you didn't actually make a string. It's easily correctable, though:
var res = "";
// ...
res += "(";
// ...
res += ")";
// ...
return res;
+ 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;
}
+ 2
Use toUpperCase or toLowerCase methods to ignore capitalization.
+ 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("(( @"),"))((");
+ 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(')');
}
}
}
+ 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;
}
+ 1
toLowerCase method is not modify origin string, it return a new string.
0
Thanks i did it ❤️❤️❤️
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
0
but,is there a way to specify the level of questiones on codeWars