skip to Main Content

I’m doing for-loop practice and can only use for loops to iterate over the arrays and strings in the problems I’m working.
QUESTION:Return and array of 2 arrays
// (1 – an array of names that contain "a" in name, 2 – an array of names that don’t have ‘a’ in name)

I know how to push the names containing the letter ‘a’ to an array but the names that don’t contain the letter a, are being pushed into the array as many times as it’s being iterated over, how can I have the array of names that doesn’t contain the letter ‘a’ only contain the name once and not as many times as its iterated over I know the issue lies somewhere is me having the for loop nested I think but I just cant see it, any tips or advice are welcome !

const arr1 = ['susan', 'meghan', 'timmy', 'tommy', 'thomas'];

function itsSomething(array) {
  let bothArrays = [];
  let withA = [];
  let withoutA = [];
  for (let i = 0; i < array.length; i++) {
    let name = array[i];
    for (let x = 0; x < name.length; x++) {
      if (name[x] == 'a') {
        withA.push(name)
      } else {
        withoutA.push(name)
      }
    }
  }
  bothArrays.push(withA, withoutA);
  return bothArrays
}

console.log(itsSomething(arr1))

2

Answers


  1. There are two problems with your code:

    1. Once you find an a you should break out of the loop. Otherwise if the word has multiple a you’ll push it to withA multiple times.
    2. You’re pushing to withoutA whenever you find a non-a character, but there could still be an a later in the string. You have to wait until the end of the word to know if it had an a in it. Use a flag variable to hold this information.
    const arr1 = ['susan', 'meghan', 'timmy', 'tommy', 'thomas'];
    
    function itsSomething(array) {
      let withA = [];
      let withoutA = [];
      for (let i = 0; i < array.length; i++) {
        let name = array[i];
        let foundA = false;
        for (let x = 0; x < name.length; x++) {
          if (name[x] == 'a') {
            withA.push(name);
            foundA = true;
            break;
          }
        }
        if (!foundA) {
          withoutA.push(name)
        }
      }
      return [withA, withoutA];
    }
    
    console.log(itsSomething(arr1))

    There’s no need for the bothArrays variable. You can simply create that array when you’re returning the two arrays.

    Login or Signup to reply.
  2. here i bring you two solutions, first the easiest solution is using the method includes, like this:

    function splitNames(array){
    let finalArray = [];
    let arraysWithA = [];
    let arraysWithoutA = [];
    for(let i = 0; i < array.length; i++){
      if(array[i].includes('a')){
        arraysWithA.push(array[i]);
      } else {
        arraysWithoutA.push(array[i]);
      }
    }
    finalArray.push(arraysWithA);
    finalArray.push(arraysWithoutA);
    return finalArray;
    

    }

    if you are practicing loops the best way is how Barmar mentioned, using a flag variable to avoid pushing unnecessary names, like this

    function splitNamesWithLoop(array){
    let finalArray = [];
    let arraysWithA = [];
    let arraysWithoutA = [];
    
    for(let i = 0; i < array.length; i++){
      let strings = array[i];
      let flagForA = false;
      for( s of strings){
        if(s === "a"){
          arraysWithA.push(array[i]);
          flagForA = true;
          break;
        } 
      }
      if(flagForA === false){
        arraysWithoutA.push(strings);
      }
    }
    finalArray.push(arraysWithA);
    finalArray.push(arraysWithoutA);
    return finalArray;
    

    }
    in terms of performance i will strongly suggest you to use the first solution it has a
    a time complexity of O(n), while the second solution has a time complexity of O(n * m) for all the nested loops. hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search