-4

I'm trying to turn this array into 2 different arrays. One array should be of names that contain the letter 'o' in them and the second array should be the rest of the names, that do not have 'o'.

I can only use the for loop and the only method I can use is .push().

Given array:

var names = ['octavia', 'peter', 'olive', 'ava', 'aiden']; 

Desire result:

var nameO = ['octavia', 'olive'];
var nameNoO = ['peter', 'ava', 'aiden'];

This is the code I came up with. Got one of the results for names with 'o', but not without 'o'.

var names = ['octavia', 'peter', 'olive', 'ava', 'aiden'];
var namesO = [];
var namesNoO = [];
for(let i in names){
  for(let j in names[i]){
    if(names[i][j] === 'o'){
      namesO.push(names[i]);
    } 
    if(names[i][j] !== 'o'){
    namesNoO.push(names[i])
    }
 }
}
console.log(namesO, namesNoO); 
3

3 Answers 3

-1

You should spend some time familiarizing yourself with all of the various functions that are part of the Array prototype.

Saying you want to "split" an array suggests you are looking for Array.prototype.slice(). However, your example suggests what you really want to do is filter your array. To do that you want to use the Array.prototype.filter() function.

const names = ['octavia', 'peter', 'olive', 'ava', 'aiden'];
const withNoO = name => !name.includes('o');
const withO = name => name.includes('o');

const nameO = names.filter(withO);
// ['octavia', 'olive'];

const nameNoO = names.filter(withNoO)
// ['peter', 'ava', 'aiden'];

I am using "arrow functions" to make the code more compact. This could also be done like this:

function withNoO(name) {
    return !name.includes('o');
}

function withO(name) {
    return name.includes('o');
}

const nameO = names.filter(withO);
// ['octavia', 'olive'];

const nameNoO = names.filter(withNoO)
// ['peter', 'ava', 'aiden'];
Sign up to request clarification or add additional context in comments.

Comments

-1

You can use the .filter function of an array to filter the items. For each string, you can use .includes to check if it includes the letter "o". The .toLowerCase function converts the input strings to lowercase before comparing them, making sure this works for both capital and lowercase letters.

var names = ['John', 'Alice', 'Bob', 'Carol', 'Oliver', 'Sophia'];

var namesWithO = names.filter(name => name.toLowerCase().includes('o'));
var namesWithoutO = names.filter(name => !name.toLowerCase().includes('o'));

Or

var names = ['John', 'Alice', 'Bob', 'Carol', 'Oliver', 'Sophia'];

var namesWithO = [];
var namesWithoutO = [];

names.forEach(name =>
{
   if (name.toLowerCase().includes('o'))
   {
      namesWithO.push(name);
   }
   else
   {
      namesWithoutO.push(name);
   }
});

10 Comments

Literally was just typing out this answer
Includes will check all the text rather than just the first letter.
Yeah as question mention its about have not start with so includes is correct here
it's O(n*2), could be O(n)
@AlexanderNenashev Strictly speaking there is no absolutely difference between O(n) and O(2n)
|
-3

You can use include for condition matching.

   var names = ['octavia', 'peter', 'olive', 'ava', 'aiden'];
   var namesO = [];
   var namesNoO = [];

  for(var i=0, l = names.length; i < l; i++) {
     if(names[i].toLowerCase().includes("o")){
      namesO.push(names[i]);
      }
  if(!names[i].toLowerCase().includes("o")){
      namesNoO.push(names[i]);
    }
    }
     console.log(namesO,namesNoO);

1 Comment

the slowest due toLowerCase() and double includes('o'). a bad choice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.