sandypockets
sandypockets

Reputation: 409

Getting undefined, and can't find the bug

I'm having trouble finding the bug in my program. The console.logs labelled "Debug 3" and "Debug 6" are both returning undefined. I'm sure it's obvious, but any help pointing them out would be much appreciated. I'm also still pretty new, so if you have any suggestions about a different way I should have done this, I'm open to the feedback!

I know I could have just written a bunch of if/else statements, but I really wanted to give an honest go at making the code modular, and as reusable as possible.

Side note: I know that my function/variable names are trash. I plan on renaming them all once the program is working properly. This was just easier to help keep track (though obviously that didn't work out very well, hence here I am).

The code I have so far is:

// ---------------------- INPUT CHECKS ---------------------- //

// Check and return the number of words supplied in the name
const checkNameLength = (name) => {
  arr = name.split(' ');
  return arr.length;
};


// Checks if the name contains any spaces on the edges
const checkForSpacesOnEdges = (name) => {
  if (name[0] === ' ' || name[-1] === ' ') {
    return true;
  } else {
    return false;
  }
};


// Checks if the name contains an honorific
const checkIfContainsHonorific = (name) => {
  let accumulator = 0;
  let result;
  for (let i = 0; i < name.length; i++) {
    if (name[i] === '.') {
      accumulator++;
    }
  }
  if (accumulator !== 0) {
    result = true;
  } else {
    result = false;
  }
  return result;
};


// Returns a 1 word string 
const isSingleName = (name) => {
  let result;
  let arr = name.split(' ');
  if (arr.length === 1) {
    result = true;
  } else {
    result = false;
  }
  return result;
};


// ---------------------- OUTPUT ---------------------- //

// Return empty string
const emptyOutput = (name) => {
  return 'empty string';
};


// Returns the exact string provided by user
const returnAsIs = (name) => {
  return name;
};


// Returns the name with trailing spaces removed
const returnWithoutSpaces = (name) => {
  return name.trim();
};


// Reverses the name string and returns it
const normalReverse = (name) => {
  let arr = [];
  let result;
  arr = name.split(' ');
  arr.reverse();
  result = arr[0] + ', ' + arr[1];
  result.toString();
  return result;
};


// Reverses the first and last name, but leaves the honorific in front
const hasHonorificReverseNames = (name) => {
  let arr = name.split(' ');
  let firstAndLastArr = [];
  for (let i = 1; i < arr.length; i++) {
    firstAndLastArr.push(arr[i]);
  }
  firstAndLastArr.reverse();
  return arr[0].toString() + firstAndLastArr[0].toString() + ', ' + firstAndLastArr[1].toString();
};


// Main func
const nameInverter = (name) => {
  let result;
  if (!name) {
    result = emptyOutput(name);

  } else if (isSingleName(name)) {
    result = returnAsIs(name);
    
  } else if (!checkIfContainsHonorific(name)) {
    if (checkForSpacesOnEdges(name)) {
      result = returnWithoutSpaces(name);
    }

  } else if (checkNameLength(name) === 1) {
    if (checkIfContainsHonorific(name)) {
      result = emptyOutput(name);
    }
    
  } else if (checkNameLength(name) === 2) {
    console.log("Flag 1: ", name);
    if (!checkIfContainsHonorific(name)) {
      console.log("Flag 2: ", name);
      result = name;
    }
    
    
  } else if (checkNameLength(name) === 2) {
    if (checkIfContainsHonorific(name)) {
      result = returnAsIs(name);
    }
    
  } else if (checkNameLength(name) === 3) {
    if (checkIfContainsHonorific(name)) {
      result = hasHonorificReverseNames(name);
    }

  } else {
    return normalReverse(name);
  }
  return result;
};


console.log("Debug 1", nameInverter(''));
console.log("Debug 2", nameInverter('Ronald'));
console.log("Debug 3", nameInverter('Ronald McDonald'));
console.log("Debug 4", nameInverter(' Ronald McDonald '));
console.log("Debug 5", nameInverter('Dr. Ronald McDonald'));
console.log("Debug 6", nameInverter('Dr. Ron'));

The output that I'm getting from those console.logs (including the Flag 1 console.log in the function I suspected was causing problems, is listed below:

Debug 1 empty string
Debug 2 Ronald
Debug 3 undefined
Debug 4 Ronald McDonald
Debug 5 Dr.McDonald, Ronald
Flag 1:  Dr. Ron
Debug 6 undefined

Thank you, any guidance is greatly appreciated!

Upvotes: 0

Views: 341

Answers (1)

Brett East
Brett East

Reputation: 4322

So you're issue here is that you're actually reaching some of your if else statements, but then not setting the code within that code block.

Anytime you're getting undefined here, is because you're not setting result to anything.

if else statements won't 'fall through' so as soon as one is true, it will run that block of code, but not the following blocks.

A good example is with your 'debug 3' 'Ronald Mcdonald' check. It goes into your if else statements, and hits the has length === 2 test, but then in that code block, you do another test to see if it contains 'Dr.' and if it does then set result to name, but if this check fails, you're never setting result to the name.

Essentially you either need to order your if else statements so that they can be checked in order and they won't conflict or you need to nest additional if else statements within those checks for length to check for the other things that you want to.

Upvotes: 1

Related Questions