0

Its a relatively simple question however I wanted to see how other people would tackle the below issue:

  1. A user can enter (x) quantity in to text box txtUserInput

so for example UserInput = 14

  1. 1 box can contain upto 4 items.

so based on the above txtUserInput, I want the program to calculate how many boxes are required, (in this case the answer would be 4 boxes, with 2 spaces remaining in the final box.

below is an example of the code in its basic form:

var itemsCount = txtUserInput;
var boxes = 0;

if (itemsCount <=4) {
boxes ++;
}

else if (itemsCount <=8 && >4) {
boxes += 2;
}

as you can see, I could repeat this over and over again to get the correct result but it would become very tedious and a lot longer than it requires...

So using JavaScript how can I put this into an array/loop and get boxes to give me my desired output?

thankyou for your help

3
  • 3
    Ever heard of division? Commented May 15, 2015 at 16:22
  • yes, but i dont want the answer to = 3.5 (which is 14 / 4) Commented May 15, 2015 at 16:23
  • @Crezzer7 Combine division and rounding up, and you have your answer. Commented May 15, 2015 at 16:24

3 Answers 3

5

Don't loop, do a division and then round up:

var boxes = Math.ceil(itemsCount / 4);
Sign up to request clarification or add additional context in comments.

4 Comments

what does that code do? i find javascript's logic very different to that of C# when it comes to thinks like this... thanks for your post
@Crezzer7: What exactly is unclear? itemsCount / 4 is division. It divides the value of itemsCount by 4. You can learn more about division on Wikipedia: en.wikipedia.org/wiki/Division_(mathematics) . Math.ceil is a function (en.wikipedia.org/wiki/Function_(mathematics) , en.wikipedia.org/wiki/Subroutine ). A function accepts an input and returns an input. This function accepts a number, rounds it up and returns the result. You can learn more about rounding on Wikipedia: en.wikipedia.org/wiki/Floor_and_ceiling_functions
For someone with C# experience as you say you have, you shoud know that Math.Ceiling(value) from C# is very similar to Math.ceil(value) from javascript.
the truncated method name really does throw you a curve ball though
1

With your approach of checking how many boxes are needed, you would use a loop instead of having code for all possible number of boxes:

var boxes = 0;
while (itemsCount > 0) {
  boxes++;
  itemsCount -= 4;
}

However, you don't need a loop for this. You can just divide the number of items by four, and make sure that any fraction in the result causes another box to be used by rounding up:

var boxes = Math.ceil(itemsCount / 4);

For example dividing 14 by four gives the result 3.5, and rounding up gives you 4.

1 Comment

thanks for another alternative... this is what i was thinking
1

You can use Math.ceil to round up after dividing the number of items by the size of the boxes (in this case 4);

var boxes = Math.ceil(txtUserInput/4);

Javascript's type coercion will handle an empty input (if txtUserInput is null) as zero automatically.

1 Comment

ok thanks, this may look like a dumb question to people who are used to Javascript, but i would do this very differently in C#, thanks for your answer

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.