0

I need help to make sure that all numbers in my array are displayed because only the first 2 are being displayed.

function tizer(n) {
  var firstArray = (""+n).split('');
  var newArray = [];
  for (var i = 0; i < firstArray.length; i++) {
    newArray[i] = parseInt(firstArray.shift(),10);
  }
  return newArray;
}

console.log(tizer(8675));

Result: [8,6]

Expected: [8,6,7,5]

3
  • Could you add some test cases? Specifically, what you used as an input to get this result. Commented Feb 16, 2019 at 16:17
  • how do I do it .I am new here . Commented Feb 17, 2019 at 20:46
  • Just edit your post and say "This is my input, this is my output, and this is what I expected. Commented Feb 17, 2019 at 22:04

2 Answers 2

1

It is much simpler than you thought. You should have used firstArray[i] instead of firstArray.shift().

function tizer(n) {
  var firstArray = (""+n).split('');
  var newArray = [];
  for (var i = 0; i < firstArray.length; i++) {
    newArray[i] = parseInt(firstArray[i],10);
  }
  
  // Sort odd first
  var arraySorted = [...newArray.filter(item => item%2 == 1), ...newArray.filter(item => item%2 == 0)]
  
  return arraySorted;
}

console.log(tizer(8675));

Sign up to request clarification or add additional context in comments.

8 Comments

You are welcome. Feel free to upvote my answer. Thanks.
If you can me out with this that will be great. I am new to javascript.JavaScript Write a mySort function which takes in an array integers, and should return an array of the inputed integers sorted such that the odd numbers come first and even numbers come last. For exampl1e: mySort( [90, 45, 66, 'bye', 100.5] ) should return [45, 66, 90, 100]
Oh, I see. I'm out of home. When I get back I can help you with that.
alright ,thanks .Will send the code I have done for it shortly.
Hello @ohbabie, I have added one line of code to sort the odds first.
|
1

Simply use a .map(Number) after you .split

function tizer(n) {
  return (n + '').split('').map(Number);
}

console.log(tizer(8675));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.