Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to display this JavaScript output horizontally?

I am trying to create a function evenoddarray() which is supposed to accept one parameter which should be an array of numbers. This function should check all the numbers in the array in order to log whether each number is even or odd.

For example, evenoddarray(1, 45,8,6,9) should display ‘odd,odd,even,even,odd’.

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function evenoddarray(array) {
  let tab = [1, 45,8,6,9];
  for (const value of tab) {
      if (value % 2 === 0) {
          console.log('even');
      } 
      else {
          console.log('odd');
      }
  }
  }
evenoddarray();

>Solution :

You just need to capture the results as you loop through your tabarray. There are many shorter versions but this may make more sense at first.

function evenoddarray() {
  let tab = [1, 45,8,6,9];
  let result = [];
  for (const value of tab) {
      if (value % 2 === 0) {
          result.push('even');
      } 
      else {
          result.push('odd');
      }
  }
  return result;
}
console.log(evenoddarray().toString());
console.log(evenoddarray().join(','));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading