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);
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 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'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))
I need to count words from prompt and write them to the array. Next I have to count their appearance and sort them.
I have code like this:
let a = window.prompt("Write sentence")
a = a.split(" ")
console.log(a)
var i = 0;
for (let i = 0; i < a.length; i++) {
a[i].toUpperCase;
let res = a[i].replace(",", "").replace(".", "")
var count = {};
a.forEach(function(i) {
count[i] = (count[i] || 0) + 1;
});
console.log(count);
document.write(res + "<br>")
}
I don't know how to connect my word with specific number for number of appearances and write this words one time.
On the end it should look like:
a = "This sentence, this stentence, this sentence, nice."
This - 3
Sentence - 3
nice - 1
If I don't misunderstood your requirements then Array.prototype.reduce() and Array.prototype.sort() will the trick for you. Imagine I got the example string from your window.prompt()
let string = `this constructor doesn't have neither a toString nor a valueOf. Both toString and valueOf are missing`;
let array = string.split(' ');
//console.log(array);
let result = array.reduce((obj, word) => {
++obj[word] || (obj[word] = 1); // OR obj[word] = (++obj[word] || 1);
return obj;
}, {});
sorted_result = Object.keys(result).sort(function(a,b){return result[a]-result[b]})
console.log(result);
console.log(sorted_result);
AS PER QUESTION EDIT
let string = `This sentence, this sentence, this sentence, nice.`;
let array = string.split(' ');
array = array.map(v => v.toLowerCase().replace(/[.,\s]/g, ''))
let result = array.reduce((obj, word) => {
++obj[word] || (obj[word] = 1); // OR obj[word] = (++obj[word] || 1);
return obj;
}, {});
console.log(result)
You can use reduce. Like so:
const wordsArray = [...].map(w => w.toLowerCase());
const wordsOcurrenceObj = wordsAray.reduce((acc, word) => {
if (!acc[word]) {
acc[word] = 0;
}
acc[word] += 1;
return acc;
}, {});
What this does is keep track of the words in an object. When a word is not there, initializes with zero. And then adds a 1 every time you encounter that word. You will end up with an object like this:
{
'word': 3,
'other': 1,
...
}
Add another loop at the end that goes over the counts and prints them:
for(let word in count) {
console.log(word + " appeared " + count[word] + " times");
}
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('/');
}
)