Javascript Error Handling HELP needed

Need help on error handling if outside of even/odd array?? In the partyOfCity.

Thanks up front…

const evenArray = ['0','6','4','2']; //Even array for assignment 
const oddArray = ['5','7','1','3']; //Odd array for assignment 

const initialObj = {  //Setup attributes for intial object from ATTUID in Webphone
  'city': 'Peoria',
  'name': 'David J Iman',
  'defaultValue': 2
}
console.log(initialObj);

let numberOfNames = wordLength(initialObj.name); //Assign number of names that is in the name attribute of the initialObj

function wordLength(str) { //Calculation for number of words in a string  
  return str.split(' ').length;                       
}

const cityLength = initialObj.city.length;  //Used to determine length of characters of the city name

const index = Math.round(cityLength/numberOfNames); //Compare index to length rounded to interger
//console.log(cityLength, '/', numberOfNames) - used for testing code

const partyOfCity = evenOrOdd(cityLength); //Create new const setting as even or odd from the value

function evenOrOdd(num) { //Used to determine even or odd                             
  if (num % 2 === 0) {                                 
      return 'even';                                  
  }else {                                             
      return 'odd'                                    
  }
}
if (partyOfCity === 'even') { 
               
  if (index > evenArray.length) {                      
      var result = Number(evenArray[evenArray.length]); 
  } else {
      var result = Number(evenArray[index]);           
  }
} else {
  if (index > oddArray.Length) {                        
      var result = Number(oddArray[oddArray.length]);
  } else {
      var result = Number(oddArray[index]);           
  }      
}
//console.log(partyOfCity) used for testing code

const finalObj = {  //Create new final object from intial object 
  'city': initialObj.city,
  'name': initialObj.name,
  'defaultValue': initialObj.defaultValue + result
}
console.log(finalObj);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Not sure what you mean by error handling. What is your code either doing or not doing that you want it to do?

2 Likes

Here’s a link to a working code thing. Your code, as is, works. Not sure what you expect it to do, or what sort of error handling you are looking to add, but it does its thing.

Well that’s cool - hadn’t realized that repl would embed the working code snippet like that. Gonna have to use that a LOT more. :wink:

It is working with current state and name but if longer it would be outside array. Need to error handle so if so show last in array or error msg.

It is working as is but if city and name where longer it would be outside array. Looking to error handle partyOfCity to show last in array or error msg.

I am not understanding what you mean by “outside array”. Can you show an example of what you are talking about?

Correct and if so I want to show last in array or even an error msg

You mean check if the index into evenArray/oddArray returns undefined?

if (!Boolean(evenArray[index]) || !Boolean(oddArray[index])) {
  console.log('out of range');
  console.log(evenArray[evenArray.length - 1]); // log last element
  console.log(oddArray[oddArray.length - 1]); // log last element
  return;
}

That is just an all-in-one type catch but you get the idea.