Questions about a specific Array exercise [Day 5] #614
-
Hi I'm a newbie coder and I've finished the exercises for the Day 5 Array except for one. It's the level 1 exercise which is to "Filter out companies which have more than one 'o' without the filter method" Questions:
The best thing that I could do out of my current knowledge is to output how many companies have more than one 'o', which is "2" using this code and I'm not really sure if that will do for this particular exercise: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I am not proficient in JavaScript but I'll try my best you could try .reduce method to iterate over elements in array and apply callback function to each element const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon']; const companiesWithMoreThanOneO = itCompanies.reduce((acc, company) => { console.log(companiesWithMoreThanOneO); |
Beta Was this translation helpful? Give feedback.
-
You can use regex too as shown below. let rege = /[A-Za-z]*(o)(o)+[a-z]*/g; [A-Za-z]* for the prefix string |
Beta Was this translation helpful? Give feedback.
I am not proficient in JavaScript but I'll try my best
Regarding whats been asked by the question : The problem is asking you to separate the company that contain more than one alphabet 'o' in them e.g: G'o'o'gle which has 2 'o' in it while 'O'racle only has one 'o'
you could try .reduce method to iterate over elements in array and apply callback function to each element
const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon'];
const companiesWithMoreThanOneO = itCompanies.reduce((acc, company) => {
if (company.split('o').length > 2) {
acc.push(company);
}
return acc;
}, []);
console.log(companiesWithMoreThanOneO);
I think this should give you correct ou…