Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Questions about a specific Array exercise [Day 5] #614

Answered by Kashyap-Aayush
erthewise asked this question in Q&A
Discussion options

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:

  1. 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?
  2. 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)

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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!

Answer selected by erthewise
Comment options

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).

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /