scrimba
Note at 1:18
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 1:18
AboutCommentsNotes
Note at 1:18
Expand for more info
main.js
run
preview
console


function differentSymbolsNaive(str) {

// 1. For loop
var unique= "";
for(var i = 0; i < str.length; i++)
{
if(unique.indexOf(str.charAt(i)) < 0)
{
unique += str.charAt(i);
}
}
//return unique.length;

//2. Using set...
var stringResult = String.prototype.concat(...new Set(str)).length

//3. Arrays
var arrayresult = str.split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join('').length;

// 4. loadash
// return _.uniq(str).join('').length; // gives 'abc'

//5. Array prototype...
var arrayprotoResult = Array.prototype.map.call(str,
(obj,i)=>{
if(str.indexOf(obj,i+1)==-1 ){
return obj;
}
}
).join("").length;

//6. While loop
var len = str.length;
var coll = [];
var a=0;
while(len--)
{
if(!coll.includes(str[len]))
{
coll.push(str[len]);
}
}

return coll.length;

}



/**
* Test Suite
*/
describe('differentSymbolsNaive()', () => {
it('returns count of unique characters', () => {
// arrange
const str = 'cabca';

// act
const result = differentSymbolsNaive(str);

// log
console.log("result: ", result);

// assert
expect(result).toBe(3);
});
});
Console
"result: "
,
3
,
/index.html
LIVE