-
Notifications
You must be signed in to change notification settings - Fork 0
Test for front end engineers
Daisho Komiyama edited this page Jun 29, 2020
·
10 revisions
I'm starting with a warm-up exercise.
// reverse('Hello'); // olleH
const testString = "Hello";
function reverse(str) {
const len = str.length;
let newstr = "";
for (let i = (len - 1); i >= 0; i--) {
newstr += str[i];
}
return newstr;
}
reverse(testString);
// reverse('Hello'); // olleH
const testString = "Hello";
function reverse(str) {
return str.split("").reverse().join("");
}
reverse(testString);
const str = "This is is a test test string";
function removeDuplicates(str) {
const arr = str.split(" ");
return arr.filter(item => {
for (let i = 0; i < arr.length; i++) {
if (item === arr[i]) {
return = item === arr[i] ? false : true;
}
}
}).join(" ");
}
removeDuplicates(str);
const str = "This is is a test test string";
function removeDuplicates(str) {
const arr = str.split(" ");
const set = new Set(arr);
const newString = [...set].join(" ");
return newString;
}
console.log(removeDuplicates(str));
const arr = [1, 2, [3, 4, [5, 6, 7], 8], 9, 10];
const newarray = [];
function flatten(arr) {
for (let i = 0; i < arr.length; i++) {
if (!Array.isArray(arr[i])) {
newarray.push(arr[i]);
}
else {
flatten(arr[i]);
}
}
return newarray;
}
console.log(flatten(arr));
const arr = [1, 2, [3, 4, [5, 6, 7], 8], 9, 10];
const newarray = [];
function flatten(arr) {
return arr.reduce((acc, item) => {
if (Array.isArray(item)) {
acc = [...acc, ...flatten(item)];
}
else {
acc.push(item);
}
return acc;
}, []);
}
console.log(flatten(arr));