Javascript Fibonacci Sequence
Iām currently reading Jeff Atwoodās book Effective Programming: More Than Writing Code. It was only $4.60 on the Kindle marketplace. I see now why the price is so low: The book is his blog, just organized into sections. Iām not complaining, itās a great book.Ā
In any case, the book has a section on phone interviewing and which questions to ask. They arenāt stupid questions likeĀ āwhy are manhole covers roundā. What does that really tell you about an interviewee anyway? Oh yeah, they can bullshit. Iām not downplaying the grand art of BSāing, itās a useful skill, but it doesnāt tell you anything about the intervieweeās programming skills. The seven questions recommended by Mr. Atwood are:
Write a function to reverse a string.
Get the nth fibonacci number.
Print out the grade school multiplication table up to 12x12.
Write a function to sum the integers from a text file. One int per line.
Print out the odd numbers 1 - 99.
Find the largest value in an array of integers.
Format an RGB value as a six digit hexadecimal string.
The one that grabbed me was the fibonacci sequence. It seemed like something every programmer should know, including me. So here it is in javascript:
function getNthFibNum(n) {
Ā var fib = [0, 1]; Ā Ā
Ā Ā for (var i = 2; i <= n; ++i) {
Ā Ā Ā fib[i] = (fib[i-1] + fib[i-2]);
Ā } Ā Ā
More info on the fibonacci sequence.
Okay, readers, whatās the most interesting question to you?Ā