2

I have an Array with some numeric values. I want to display them but I also have a user input which should be displayed after the respective element in the array. My approach works fine but seems really overcomplicated to me. I created a fiddle:

http://jsfiddle.net/sjoy8p59/1/

var numbers = [2, 4, 8, 12, 42, 120, 612, 69];
var input = 32;
var flag = false;
var output = "";

for (i = 0; i < numbers.length; i++) {
    if (numbers[i] < input) {
        output += numbers[i]+", ";
    } else {
        if (flag == false) {
            output += input + ", ";
            flag = true;
        } else {
            output += numbers[i]+", ";
        }
    }
}
document.getElementById("output").innerHTML = output;

So the value 32 from input should be displayed after the 12 and before the 42 from the array. I use a for loop and a boolean with an if clause to detect whether the Array element is larger or smaller than the input and then add it all together and display it.

How can I do it better and cleaner?

EDIT: I figured out there is a mistake in my code because it skipped one element in the array

6
  • should I post all the code from the fiddle? Commented Sep 12, 2015 at 20:46
  • The array you've provided isn't sorted - if you sort the array every time you push an element to it, then it'll be at the right place :) Commented Sep 12, 2015 at 20:46
  • Is it better to add the input to the array first and then display it or directly display the array and put the input somewhere in there? Commented Sep 12, 2015 at 20:49
  • Where should the value be added in the array? And is the array always sorted? For example, where should 32 be added in the following array: [2,12,42,612,3,13,43] Commented Sep 12, 2015 at 20:49
  • 1
    Oh i understand there is a mistake in my logic...yes the array must be sorted and then the input must been added to the right place like array element < input < array element Commented Sep 12, 2015 at 20:54

3 Answers 3

2

Working fiddle.

You can do it using push() to add your input to the Array, and use simple function sortNumber() to sort it.

JS :

var numbers = [2, 4, 8, 12, 42, 120, 612, 69];
var input = 32;

numbers.push(input); //add 32 to the array

function sortNumber(a,b) {
    return a - b;
}

numbers.sort(sortNumber); //Sort Array
document.getElementById("output").innerHTML = numbers.join(","); //Display result

var numbers = [2, 4, 8, 12, 42, 120, 612, 69];
var input = 32;

numbers.push(input); //add 32 to the array

function sortNumber(a,b) {
  return a - b;
}

numbers.sort(sortNumber);

alert(numbers.join(","));

Hope this helps.

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

Comments

2

I'd just sort them:

var numbers = [2, 4, 8, 12, 42, 120, 612, 69];
var input = 32;
numbers.push(input)
numbers.sort(function(a,b){return a-b});
document.getElementById("output").innerHTML = numbers.join(', ');
<div id="output"></div>

Comments

0

https://jsfiddle.net/sjoy8p59/3/

var numbers = [2, 4, 8, 12, 42, 120, 612, 69];
var input = 32;
var flag = false;
var output = "";

for (i = 0; i < numbers.length; i++) {
    if (numbers[i] < input || flag == true) {
        output += numbers[i]+", ";
    } else {
        output += input + ", ";
        flag = true;
        i--
    }
}
document.getElementById("output").innerHTML = output;

I edited your jsfiddle with minimal edit to your code, although the sort methods look cleaner.

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.