I'm trying to see why my function keeps returning undefined?
The output should be the same string but with every word with over 4 characters reversed?
Sorry I'm still a noob with JavaScript and this is driving me crazy lol
Thank you!
const reverseString = (data) => {
data = data.split(' '); //convert to array
data = data.forEach(function flipper (x) {
x = x.split('');
if (x.length > 4) {
x = x.reverse();
}
x = x.join('');
});
return console.log(data);
}
reverseString('Hello World thank you so much');
First of all forEach returns undefined so you cann't assing directly to source array.
As you need to assign the reverse string to source array. It would be better to use third parameter(i.e source array) of callback function in forEach
forEach
const reverseString = (data) => {
data = data.split(" "); //convert to array
data.forEach(function flipper(x, i, src) {
x = x.split("");
if (x.length > 4) {
x = x.reverse();
}
x = x.join("");
src[i] = x;
});
return data.join(" ");
};
const result = reverseString("Hello World thank you so much");
console.log(result);
It is easy to achieve this using map
const reverseString = (data) => {
return data
.split(" ")
.map(x => x.length > 4 ? x.split("").reverse().join("") : x)
.join(" ");
};
const result = reverseString("Hello World thank you so much");
console.log(result);
In your code, you assign the returned value from forEach() to the data variable.
the function forEach returns nothing but undefined
Check the MDN doc
You may use the map function in the other answer.
As an alternative, your function can be re-written as follows:
const reverseString =
data => data
.split(" ")
.map(word => word.length > 4 ? word.split("").reverse().join("") : word)
.join(" ");
Another approach with a regex in replace() with a replacement callback
const reverseString = str => str.replace(/\S{5,}/gi, (s) => [...s].reverse().join(''))
const str ='Hello World thank you so much';
console.log(reverseString(str))
Related
i have an array ["academy"] and i need count chars from the string in the array.
output:
a:2
c:1
d:1
e:1
m:1
y:1
like this
i tried two for loops
function sumChar(arr){
let alph="abcdefghijklmnopqrstuvxyz";
let count=0;
for (const iterator of arr) {
for(let i=0; i<alph.length; i++){
if(iterator.charAt(i)==alph[i]){
count++;
console.log(`${iterator[i]} : ${count}`);
count=0;
}
}
}
}
console.log(sumChar(["abdulloh"]));
it works wrong
Output:
a : 1
b : 1
h : 1
undefined
Here's a concise method. [...new Set(word.split(''))] creates an array of letters omitting any duplicates. .map takes each letter from that array and runs it through the length checker. ({ [m]: word.split(m).length - 1 }) sets the letter as the object key and the word.split(m).length - 1is a quick way to determine how many times that letter shows up.
const countLetters = word => (
[...new Set(word.split(''))].map(m => ({
[m]: word.split(m).length - 1
})))
console.log(countLetters("academy"))
You can check the occurrences using regex also. in this i made a method which checks for the character in the string. Hope it helps.
word: string = 'abcdefghijklkmnopqrstuvwxyzgg';
charsArrayWithCount = {};
CheckWordCount(): void {
for(var i = 0;i < this.word.length; i++){
if(this.charsArrayWithCount[this.word[i]] === undefined){
this.charsArrayWithCount[this.word[i]] = this.charCount(this.word, this.word[i]);
}
}
console.log(this.charsArrayWithCount);
}
charCount(string, char) {
let expression = new RegExp(char, "g");
return string.match(expression).length;
}
You can simply achieve this requirement with the help of Array.reduce() method.
Live Demo :
const arr = ["academy"];
const res = arr.map(word => {
return word.split('').reduce((obj, cur) => {
obj[cur] = obj[cur] ? obj[cur] + 1 : 1
return obj;
}, {});
});
console.log(res);
I think this is the simplest:
const input = 'academy';
const res = {};
input.split('').forEach(a => res[a] = (res[a] ?? 0) + 1);
console.log(res);
I don't understand why when I return element.toUpperCase()
it doesn't return it back to the array as uppercase, however if I console.log(element.toUpperCase()) before that return statement it displays as upper case
const sentence = "fur pillows are hard to actually sleep on";
const uppercaseOddWords = (string) => {
string = string.split(" ");
oddWords = string.filter((element, index) => {
if (index % 2 === 0) {
return element;
}
return element.toUpperCase();
});
console.log(oddWords.join(" "));
};
uppercaseOddWords(sentence);
filter callback expected to return truthy or falsy value, in your case all values with be truthy and will not filter anything, what you are looking for is map method.
const sentence = "fur pillows are hard to actually sleep on";
const uppercaseOddWords = (string) => {
string = string.split(" ");
oddWords = string.map((element, index) => {
if (index % 2 === 0) {
return element;
}
return element.toUpperCase();
});
console.log(oddWords.join(" "));
};
uppercaseOddWords(sentence);
.filter() should only be used if you want to pick elements of the array and leave out something based on a boolean logic test. The function expects a logical condition, and can either return true or false. You are using the function wrong.
.map() on the other hand is used to transform the elements in the array from their original state to any other state based on the logic in the function.
const sentence = "fur pillows are hard to actually sleep on";
const uppercaseOddWords = (string) => {
const words = string.split(" ");
const oddWords = words.map((word, index) => {
if (index % 2 === 0) {
return word;
}
return word.toUpperCase();
});
console.log(oddWords.join(" "));
};
uppercaseOddWords(sentence);
Still wondering why returning it doesn't work, but I got the function to work this way;
const sentence = "fur pillows are hard to actually sleep on";
const uppercaseOddWords = (string) => {
let newString = "";
string = string.split(" ");
string.filter((element, index) => {
if (index % 2 === 0) {
return (newString += element + " ");
}
return (newString += element.toUpperCase() + " ");
});
console.log(newString.trim()); //remove trailing spaces
};
uppercaseOddWords(sentence);
You can achieve this by using Array.map() method which will return same number of elements as passed, Based on the condition for odd elements, We can convert odd elements in an upper case.
Live Demo :
const sentence = "fur pillows are hard to actually sleep on";
const uppercaseOddWords = (string) => {
string = string.split(" ");
const res = string.map((element, index) => (index % 2 === 0) ? element.toUpperCase() : element);
console.log(res.join(" "));
};
uppercaseOddWords(sentence);
I have a array of string.
let arr=["robin","rohit","roy"];
Need to find all the common character present in all the strings in array.
Output Eg: r,o
I have tried to create a function for above case with multiple loops but i want to know what should be the efficient way to achive it.
Here's a functional solution which will work with an array of any iterable value (not just strings), and uses object identity comparison for value equality:
function findCommon (iterA, iterB) {
const common = new Set();
const uniqueB = new Set(iterB);
for (const value of iterA) if (uniqueB.has(value)) common.add(value);
return common;
}
function findAllCommon (arrayOfIter) {
if (arrayOfIter.length === 0) return [];
let common = new Set(arrayOfIter[0]);
for (let i = 1; i < arrayOfIter.length; i += 1) {
common = findCommon(common, arrayOfIter[i]);
}
return [...common];
}
const arr = ['robin', 'rohit', 'roy'];
const result = findAllCommon(arr);
console.log(result);
const arr = ["roooooobin","rohit","roy"];
const commonChars = (arr) => {
const charsCount = arr.reduce((sum, word) => {
const wordChars = word.split('').reduce((ws, c) => {
ws[c] = 1;
return ws;
}, {});
Object.keys(wordChars).forEach((c) => {
sum[c] = (sum[c] || 0) + 1;
});
return sum;
}, {});
return Object.keys(charsCount).filter(key => charsCount[key] === arr.length);
}
console.log(commonChars(arr));
Okay, the idea is to count the amount of times each letter occurs but only counting 1 letter per string
let arr=["robin","rohit","roy"];
function commonLetter(array){
var count={} //object used for counting letters total
for(let i=0;i<array.length;i++){
//looping through the array
const cache={} //same letters only counted once here
for(let j=0;j<array[i].length;j++){
//looping through the string
let letter=array[i][j]
if(cache[letter]!==true){
//if letter not yet counted in this string
cache[letter]=true //well now it is counted in this string
count[letter]=(count[letter]||0)+1
//I don't say count[letter]++ because count[letter] may not be defined yet, hence (count[letter]||0)
}
}
}
return Object.keys(count)
.filter(letter=>count[letter]===array.length)
.join(',')
}
//usage
console.log(commonLetter(arr))
No matter which way you choose, you will still need to count all characters, you cannot get around O(n*2) as far as I know.
arr=["robin","rohit","roy"];
let commonChars = sumCommonCharacters(arr);
function sumCommonCharacters(arr) {
data = {};
for(let i = 0; i < arr.length; i++) {
for(let char in arr[i]) {
let key = arr[i][char];
data[key] = (data[key] != null) ? data[key]+1 : 1;
}
}
return data;
}
console.log(commonChars);
Here is a 1 liner if anyone interested
new Set(arr.map(d => [...d]).flat(Infinity).reduce((ac,d) => {(new RegExp(`(?:.*${d}.*){${arr.length}}`)).test(arr) && ac.push(d); return ac},[])) //{r,o}
You can use an object to check for the occurrences of each character. loop on the words in the array, then loop on the chars of each word.
let arr = ["robin","rohit","roy"];
const restWords = arr.slice(1);
const result = arr[0].split('').filter(char =>
restWords.every(word => word.includes(char)))
const uniqueChars = Array.from(new Set(result));
console.log(uniqueChars);
I am trying to capitalise the first character of each word and join all words into one string. I have managed to capitalise the first character of each word but cant seem to get .join() to work on the final result
function generateHashtag (str) {
let split = str.split(' ')
for(let i = 0; i < split.length; i++){
let finalResult = split[i].charAt(0).toUpperCase() + split[i].substring(1)
console.log(finalResult.join(''))
}
}
console.log(generateHashtag('Hello my name is')) should return ('HelloMyNameIs')
Achieving this by split is possible. first create an array of divided strings (by the delimiter ' ') and then loop around the array and capitalize the first char using the method toUpperCase and concat the rest of the string without the first letter using slice
function generateHashtag(str) {
let split = str.split(' ');
for (let i = 0; i < split.length; i++) {
split[i] = split[i].charAt(0).toUpperCase() + split[i].slice(1);
}
return split.join('');
}
console.log(generateHashtag('Hello my name is'));
More about split - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
you can do split[i] = split[i].charAt(0).toUpperCase() + split[i].substring(1) in the loop then outside loop do split.join('')
Basically you are replacing each word (split[i]) with capitalised word. Then in the end join the words.
finalResult is a String, not an Array so there is no join function.
Use this instead :
function generateHashtag (str) {
let arrayWords = str.split(' ')
const titleCasedArray = arrayWords.map(word => titleCaseWord(word))
return titleCasedArray.join('');
}
function titleCaseWord (word){
return word.slice(0,1).toUpperCase() + word.slice(1,-1).toLowerCase()
}
You can do something like this:
function generateHashtag (str) {
//returns array of strings
let split = str.split(' ')
//returns array of strings with each word capitalized
const capitalizedWordsArr = split.map( word => word.charAt(0).toUpperCase() + word.substring(1))
//returns a string by joining above array with no spaces
return capitalizedWordsArr.join('')
}
This is a perfect use-case for Array.prototype.reduce:
function generateHashtag(str) {
return str
.split(' ')
.reduce((acc, [firstLetter, ...rest]) => acc += `${firstLetter.toUpperCase()}${rest.join('')}`,
''
);
}
console.log(generateHashtag('Hello my name is')); // should return ('HelloMyNameIs')
Javascript strings are immutable so you cannot overwrite them on the go, but you can overwrite array elements.
By using String.prototype.substr() you can extract a part of the string, you can use these parts, modify it and create a new string then replace the old array element. finally returning the joined string like you wanted to
function generateHashtag(str) {
const split = str.split(' ') // array of words
for (let i = 0; i < split.length; i++)
split[i] = split[i].substr(0, 1).toUpperCase() + split[i].substr(1); // overwriting existing elements with Titlecased words
return split.join(''); // returning final string
}
console.log(generateHashtag('Hello my name is'))
You don't need to use join at all, just declare and initialize finalResult outside the loop and concatenate each word inside the loop:
function generateHashtag(str) {
const split = str.split(' '); // Array<String>
let finalResult = ''; // String
for(let i = 0; i < split.length; i++) {
const titleCased = split[i].charAt(0).toUpperCase() + split[i].substring(1);
finalResult += titleCased;
}
return finalResult;
}
console.log(generateHashtag('Hello my name is'));
However, you can simplify this code considerably by using a functional-programming (FP) style with map and reduce. See below.
I've also changed your code to use toLocaleUpperCase instead of toUpperCase and uses [0] for brevity.
It's still safe to use substring(1) for single-character strings, it just returns ''.
function generateHashtag(str) {
return ( str
.split(' ')
.map( word => word[0].toLocaleUpperCase() + word.substring(1).toLocaleLowerCase() )
.reduce( ( word, concat ) => concat + word, "" )
);
}
I forgot that join() can still be used instead of reduce (and will have an optimized implementation inside the JS engine anyway):
I've also moved the map function's logic to a named function toTitleCase.
function generateHashtag(str) {
const toTitleCase( word ) => word[0].toLocaleUpperCase() + word.substring(1).toLocaleLowerCase();
return ( str
.split(' ')
.map( word => toTitleCase( word ) ) // or just `.map( toTitleCase )`
.join()
);
}
The return statement has parens to prevent unwanted automatic-semicolon-insertion which would otherwise break the function.
If you want something similar to your code, but working, i would do this:
function generateHashtag (str) {
let split = str.split(' ')
let newStr = []
for (let i = 0; i < split.length; i++){
newStr.push(split[i].charAt(0).toUpperCase() + split[i].substring(1))
}
return newStr.join('')
}
You could also choose to do this task using a 'regular expression'.
https://cheatography.com/davechild/cheat-sheets/regular-expressions/
Here is a quick implementation:
const generateHashtag = str => {
// regular expression to capitalize the words
const regEx = /(\b[a-z](?!\s))/g
str = str.replace(regEx, (char) => {
return char.toUpperCase()
});
// remove spaces, return
return str.split(' ').join('')
}
Same code, but with less readability:
const generateHashtag = str => {
return str.replace(/(\b[a-z](?!\s))/g, (char) => {
return char.toUpperCase()
}).split(' ').join('');
}
function generateHashtag (str) {
return str.replace(/\b\S/g, e => e.toUpperCase()).replace(/\s/g,'');
}
console.log(generateHashtag('Hello my name is'))
\b: bondary \S: non space \s: space.
https://regex101.com/
//try this code solve your problem
const generateHashtag = str => {
let split = str.split(' ')
let finalResult = []
for (word of split) {
finalResult.push(word[0].toUpperCase() + word.substring(1))
}
return finalResult.join('')
}
console.log(generateHashtag('Hello my name is'))
I'm trying to write a JavaScript function to split a string by its delimiter ('/') and wanted to return its path combination in an array.
Input:
"Global/Europe/UK/London"
Desired Output:
["Global","Global/Europe","Global/Europe/UK"]
I tried the below recursive function, but for some reason the array contains only a single value.
function splitPath(str) {
var output = [];
if (str.lastIndexOf('/') !== -1) {
str = str.split("/").slice(0, -1).join("/");
output.push(str);
splitPath(str);
}
//console.log(output);
return output;
}
Please let me know if there's any straight way to achieve this in JavaScript.
Thanks.
Using Array#reduce
let input = "Global/Europe/UK/London";
let output = input.split("/").slice(0, -1).reduce((acc, item, index) => {
acc.push(index ? acc[index - 1] + '/' + item : item);
return acc;
}, []);
console.log(output);
You could split the string and map with the parts from the beginning by using only the parts without the last one.
var string = "Global/Europe/UK/London",
grouped = string
.split('/')
.slice(0, -1)
.map(function (_, i, a) {
return a.slice(0, i + 1).join('/');
});
console.log(grouped);
Here's a way to split and then iteratively add the parts:
function splitPath(str) {
const parts = str.split("/");
let head = parts.shift();
const result = [head];
for (const part of parts) {
head += "/" + part;
result.push(head);
}
return result;
}
console.log(splitPath("Global/Europe/UK/London"))
How about this ?
let output = [];
let input = str.split('/');
input.reduce((acc,v, i, arr) => { output.push(acc + "/"+ v ); return acc + "/"+ v; })
'Global/Europe/UK/London'
.split('/')
.map(
(item, i, items) => {
return items.slice(0, i+1).join('/');
}
)