JavaScript Calculation Beer 2 yuan a bottle, 4 cover for a bottle, 2 bottles for a bottle, 10 yuan to drink the most bottles

Source: Internet
Author: User

Tag: Push otherwise performs function nbsp Loop log Assignment Func

var n = 0//How many bottles are left now plus how many bottles are won?

var x = 5//Initial number of bottles

var y = 0//How many bottles are left in addition to the drink

var z = total number of 0;//

var arr = []//defines an array of stored values

Arr.push (x);//Add the initial bottle number to the array first

Procedure 1

var fn = function (s) {

y = s-4; How many bottles are left?

s = y + parseint (S/4) + parseint (S/2);//How many bottles are left plus how many are won?

n = s//assignment to the above n

Arr.push (n);//Add to Array

if (n>=4) {//If there are many bottles left plus how many bottles are greater than or equal to 4

FN (n);//Continue executing program 1

}else{//otherwise

FN1 (n)//Execute Program 2

}

}

Procedure 2

var fn1 = function (s) {

y = How many bottles are left in s-2;//

s = y + parseint (S/2);//How many bottles are left plus how many are won?

n = s//assignment to the above n

Arr.push (n);//Add to Array

if (n>=2) {//If there are many bottles left plus how many bottles are greater than or equal to 2

FN1 (n);//Continue to execute program 2

}

}

FN (x)//Execute Program 1

for (Var i=0;i<arr.length;i++) {

z + = arr[i]//Loop array add up all the numbers

}

Console.log (z);//Get Totals and Print

Logic:

1, first drink 4 bottles, the remaining 1 bottles, plus 2 bottles and 1 bottles, left 4

2, then drink 4 bottles, 0 bottles left, plus 2 bottles and 1 bottles, leaving 3 bottles.

3, then drink 2 bottles, the remaining 1 bottles, plus 1 bottles, the remaining 2 bottles

4, then drink 2 bottles, the remaining 0 bottles, plus 1 bottles, the remaining 1 bottles

5, add up the number is, 5+4+3+2+1 = 15

JavaScript Calculation Beer 2 yuan a bottle, 4 cover for a bottle, 2 bottles for a bottle, 10 yuan to drink the most bottles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.