-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Questions about a specific Array exercise [Day 5] #614
-
const itCompanies = ['Facebook', 'Google', 'Microsoft', 'Apple', 'IBM', 'Oracle', 'Amazon']
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:
- I'm not a native english speaker so I'm a bit confused on what is the desired output of the author. Is it the string Facebook, Google?
- If so, can anyone guide me on how to do it? I did a lot of trial and error with .match, .includes, and .search but to no avail. I really want to move on to the next day but this particular exercise keeps bothering me.
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:
console.log(itCompanies.join(', ').match(/oo/g).length)
Beta Was this translation helpful? Give feedback.
All reactions
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...
Replies: 2 comments 1 reply
-
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 output.
Beta Was this translation helpful? Give feedback.
All reactions
-
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 output.
Awesome! .reduce isn't mentioned yet in the lectures so I have no idea what it does but
if (company.split('o').length > 2) {
acc.push(company);
}
This code is awesome. I haven't thought of using the .split like this to check repeated letters. Thank you so much!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
You can use regex too as shown below.
let rege = /[A-Za-z]*(o)(o)+[a-z]*/g;
let s = itcompanies.join(" ");
console.log(s.match(rege));
[A-Za-z]* for the prefix string
(o) as it should contain one 'o'
(o)+ as there can be one or more 'o's
[a-z]* for suffix string (all small because we can't have capitalization in the middle of the string).
/g for a global search (doesn't stop at first solution).
Beta Was this translation helpful? Give feedback.