This is my role:
let longest = (s1, s2) => {
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
console.log(res2);
};
This is my role:
let longest = (s1, s2) => {
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
console.log(res2);
};
Source >
Com reduces
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
const remDup= s=> s.split("").sort().reduce((a,b)=>(a[a.length-1]!=b)?(a+b):a,"")
console.log(remDup(res2))
Com filter:
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
const remDup= s=> s.split("").filter((e,i,f)=>f.indexOf(e)==i).sort().join("")
console.log(remDup(res2))
With map
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
const remDup= s=> s.split("").map((c,i,o)=>(o.indexOf(c)==i)?c:"").sort().join("")
console.log(remDup(res2))
You can use the new
spread
operator of JavaScript withSet
to get an array of unique values.The Set object allows that you store unique values of any type
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
const remDup= e => [...new Set(e)].sort().join("");
console.log(remDup(res2))
The Spread Operator basically converts an array to arguments, it is very useful when you need to break an array to pass its values to a function or constructor of an object as separate value arguments. To illustrate in practice, let's create a simple addition function, which needs 2 arguments as an input parameter in its function:
function soma(a, b) {
return a + b;
}
If you intend to use this function you can simply do
soma(1, 2); // retorna: 3
What if you plan to use an array? How to pass 2 values from an array as an argument? The most obvious way would be:
var arr = [1, 2];
soma(arr[0], arr[1]); // retorna: 3
Are you more elegant? Has! You can use soma.apply(null, arr)
to invoke this function:
var arr = [1, 2];
soma.apply(null, arr); // retorna: 3
With the Spread operator
var arr = [1, 2];
soma(...arr); // retorna: 3
Basically the regex you need is this: replace(/(.)(?=.*)/g, "")
Your code looks like this:
let longest = (s1, s2) => {
var s1 = 'xyaabbbccccdefww';
var s2 = 'yestheyarehere';
let res2 = s1.concat(s2);
let str = res2.replace(/(.)(?=.*)/g, "");
console.log(str);
};