# Getting more Problem Solving Practice in Javascript?

Page 1 of 1

## 12 Replies - 4120 Views - Last Post: 29 August 2013 - 07:44 PM

### #1 AndreeU17

Reputation: 6
• Posts: 153
• Joined: 29-June 13

# Getting more Problem Solving Practice in Javascript?

Posted 16 August 2013 - 11:21 PM

Alright well im usually learning java, but just a few days ago decided to jump into javascript for no particular reason but for the enjoyment of coding and making my brain hurt repetitive />. I decided to embark into this critical thinking problem from Euler Website.

This is the problem: Euler 2nd Problem
In case your lazy to go into the site here is the question in code tags />
```Even Fibonacci Sequence

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

```

Now for personal reason i decided to make this problem a bit easy. I eliminated the even problem and adding 10 integer max making the equation simpler and more understanding for me.

So the question now is this

```Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the first 10 terms. The 1st term would be a random integer multiply by 5.

```

Okay so here is my brain working:D

I created a function like so
```function fiboNumSeq(){

var firstTerm = Math.floor(Math.random() * 5);
var arrayList = new Array(9);
arrayList[0] = firstTerm;
}
fiboNumSeq();

```

Thats what i have so far.

Now i could simply create a bunch of console.log(); adding the array terms in there but i find that coding messy. Im trying to use what i know to solve this. A switch statement wont really solve this neither would an if/else statement since im not trying to prove anything rather solve. I was thinking of a do/while, while, or for loop. What do you think?

Having issues figuring it out ? Any help is appreciated and if i left out any important information, then tell me so i can clarify />

PS: i was also thinking that i can for example find term 1 then multiply it times term 2, then term 3, etc. until i reach term 10 but like i mention that just seems to sloppy and not coder friendly.

PS: I just realize that to solve this i need a 2nd term to multiply against 1, well the second term would be another random digit (Not a decimal but rather an whole number ).
thank you

This post has been edited by AndreeU17: 16 August 2013 - 11:27 PM

Is This A Good Question/Topic? 0

## Replies To: Getting more Problem Solving Practice in Javascript?

### #2 Dormilich

• 痛覚残留

Reputation: 3984
• Posts: 12,279
• Joined: 08-June 10

## Re: Getting more Problem Solving Practice in Javascript?

Posted 17 August 2013 - 03:32 AM

currently, your function just runs and does nothing you can use further down the programme. you need at least some kind of return value.
```// using return
function fiboNumSeq() {
return Math.floor(Math.random() * 5);
}

// modifying a given array
function fiboNumSeq(arr) {
/* test for an array
if (!(arr instanceof Array)) {
throw new Error("Passed parameter is not an array");
}
*/
// thanks to pass-by-reference:
arr.push(Math.floor(Math.random() * 5));
}
```

another point you should adapt to is not using the constructors Array and Object, since there are literals (Array: [], Object: {}) for that preventing much of the complications you can get into with the constructors (e.g. predefining arrays easily leads to sparse arrays (arrays with holes)).

to create a couple of numbers you could certainly employ some kind of loop. though keeping close to the original problem (as you mentioned) you just need two numbers that you can add up later.

### #3 Dormilich

• 痛覚残留

Reputation: 3984
• Posts: 12,279
• Joined: 08-June 10

## Re: Getting more Problem Solving Practice in Javascript?

Posted 17 August 2013 - 03:42 AM

hm, just being curious.
Spoiler

This post has been edited by Dormilich: 17 August 2013 - 03:50 AM
Reason for edit:: hey, my 8,000th post

### #4 JoeSimmons

Reputation: 2
• Posts: 14
• Joined: 10-August 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 18 August 2013 - 01:09 AM

I would use a 'for' loop, personally.

```// give it a starting number and a max iteration count (not max fibo number)
// fibonacci(start, maxIter)
function fibonacci(last, max) {
var next = 0, values = [], i;

for (i = 0; i < max; i += 1) {
last += next;
next = last - next;
values.push(last);
}

return values.join(', ');
}

// returns a string. you could make it return an array instead, though
fibonacci(1, 10);
```

Not sure why I can't edit posts but...

This post has been edited by Dormilich: 18 August 2013 - 03:42 AM
Reason for edit:: edited for you

### #5 Dormilich

• 痛覚残留

Reputation: 3984
• Posts: 12,279
• Joined: 08-June 10

## Re: Getting more Problem Solving Practice in Javascript?

Posted 18 August 2013 - 03:44 AM

Quote

Not sure why I can't edit posts but...

not enough posts. this was made so that a newbie (to the forum) does not ask a question, gets it answered and then immediately deletes it.

### #6 AndreeU17

Reputation: 6
• Posts: 153
• Joined: 29-June 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 18 August 2013 - 07:49 AM

JoeSimmons, on 18 August 2013 - 02:09 AM, said:

I would use a 'for' loop, personally.

```// give it a starting number and a max iteration count (not max fibo number)
// fibonacci(start, maxIter)
function fibonacci(last, max) {
var next = 0, values = [], i;

for (i = 0; i < max; i += 1) {
last += next;
next = last - next;
values.push(last);
}

return values.join(', ');
}

// returns a string. you could make it return an array instead, though
fibonacci(1, 10);
```

Not sure why I can't edit posts but...

yeah i was thinking the same with a for loop i just didnt know how to implemented it all togetyer in a proper way.

### #7 JoeSimmons

Reputation: 2
• Posts: 14
• Joined: 10-August 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 20 August 2013 - 07:49 PM

Dormilich, on 18 August 2013 - 05:44 AM, said:

not enough posts. this was made so that a newbie (to the forum) does not ask a question, gets it answered and then immediately deletes it.

Oh, I see. How many posts do I need, then?

• Let it rip!

Reputation: 466
• Posts: 1,988
• Joined: 30-April 10

## Re: Getting more Problem Solving Practice in Javascript?

Posted 20 August 2013 - 08:03 PM

Quote

Below is the complete breakdown for edit times:
Just Registered [0 Posts] Can't Edit
New Members [1-15 posts] Can't Edit
Members [16-50 posts] 15 minutes
Active Members [50+ posts] 24 Hours
Contributors 1 Week
Authors 1 Month
Experts 1 Month
Mentors 3 Months

### #9 JoeSimmons

Reputation: 2
• Posts: 14
• Joined: 10-August 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 21 August 2013 - 10:01 PM

Oh, bummer. I guess I'll have to double post if necessary until 15 posts. It's a good rule though; I see how that could be abused.

• Let it rip!

Reputation: 466
• Posts: 1,988
• Joined: 30-April 10

## Re: Getting more Problem Solving Practice in Javascript?

Posted 22 August 2013 - 10:11 AM

The best thing to do is use the preview button until it is how you want it to show.

### #11 JoeSimmons

Reputation: 2
• Posts: 14
• Joined: 10-August 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 29 August 2013 - 12:31 PM

I preview a lot. Sometimes I just forget to add things, and I happen to remember right after posting. Surely I can't be the only one.

### #12 modi123_1

• Suitor #2

Reputation: 11521
• Posts: 45,478
• Joined: 12-June 08

## Re: Getting more Problem Solving Practice in Javascript?

Posted 29 August 2013 - 12:42 PM

JoeSimmons, on 22 August 2013 - 12:01 AM, said:

Oh, bummer. I guess I'll have to double post if necessary until 15 posts. It's a good rule though; I see how that could be abused.

I would appreciate you not just spamming until you get 15 posts. Thanks.

JoeSimmons, on 29 August 2013 - 02:31 PM, said:

I preview a lot. Sometimes I just forget to add things, and I happen to remember right after posting. Surely I can't be the only one.

Certainly.. and then you post a reply after the fact indicating your corrects.

### #13 JoeSimmons

Reputation: 2
• Posts: 14
• Joined: 10-August 13

## Re: Getting more Problem Solving Practice in Javascript?

Posted 29 August 2013 - 07:44 PM

modi123_1, on 29 August 2013 - 02:42 PM, said:

I would appreciate you not just spamming until you get 15 posts. Thanks.

I didn't say I would spam. If I post a reply and then I want to add something, I'll have to double-post since I can't edit yet.