Skip to content

Instantly share code, notes, and snippets.

@buymed-hoangpham
Created September 23, 2022 10:51
Show Gist options
  • Save buymed-hoangpham/b3d2ae29e26ed2e082630608be2b124d to your computer and use it in GitHub Desktop.
Save buymed-hoangpham/b3d2ae29e26ed2e082630608be2b124d to your computer and use it in GitHub Desktop.
Homework
//1.
const subLength = (str, char) => {
let charInStr = [],
lowerStr = str.toLowerCase().split(''),
length = 0;
lowerStr.forEach((val, index) => {
if (val === char) {
charInStr.push(index);
}
});
if (charInStr.length !== 2) {
return length;
}
return str.slice(charInStr[0], charInStr[1]).length + 1;
};
// 2.
const groceries = arr => {
const len = arr.length;
if (len == 1)
return arr[0].item;
else if (len == 2)
return arr[0].item + ' and ' + arr[1].item;
let str = '';
arr.forEach((obj, indx) => {
if (indx + 1 == len)
str += `and ${obj.item}`;
else if (indx + 2 == len)
str += `${obj.item} `;
else
str += `${obj.item}, `;
})
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment