javascript recursion rewriting with iterative array stack [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Need a template for rewriting a recursive javascript function as an iterative array stack. For hope that this approach is faster than standard recursion and would use less memory. I use object references as parameters. My function is negascout, but I'd love to reverse engineer the smaller and elegant Fibonacci_sequence.
A Simpler example would be to rewrite Fibonacci_sequence.
From rosettacode for Fibonacci_sequence
function fibonacci(n) {
if (n < 2){
return 1;
}else{
return fibonacci(n-2) + fibonacci(n-1);
}
}
console.log(fibonacci(7));
//Returns 21

An iterative way can be:
function iterativeFibonacci(n){
if (n < 2){
return 1;
}
var i;
var fibs = new Array();
fibs.push(0);
fibs.push(1);
for(i=0; i<=n; i++){
fibs.push(fibs[0] + fibs[1]);
fibs.shift();
}
return fibs[0];
}
document.write(iterativeFibonacci(7));

Related

how to do a loop in JS with 2 breaks [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to do a loop with JS that has two breaks. For example, 1 to 9, and 15 to 29. Maybe a double loop?
Its a noob question I know, but i am a beginner.
Thank you.
You can do it using a for loop like this:
for (var a = 0; a < 30; a++) {
if (a >= 1 && a <= 9) {
console.log("do this");
}
if (a >= 15 && a <= 29) {
console.log("do that");
}
}

Javascript - Sum solution [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am getting below line in my source file and I would like to sum those values separated by pipe ("|")
There is no limit for the values coming in the line (might be 100 values)
10|20|30|40|[no limit for values] - Separator is pipe "|"
The ouptput would be 100
Please help to write a javascript function for the above query.
Regards
Jay
You should take a look at JavaScript's built-in functions: With split you can split your string into an array, with reduce you can 'reduce' your array to a single value, in that case via summation. These two links should provide you enough information for building your code.
You could try something like below:
function sum(value){
var total = 0;
var array = value.split(",").map(Number);
for( var i = 0; i < array.length; i++) {
total += array[i];
}
alert(total);
}
var value = "10|20|30|40".replace(/\|/g, ',');
console.log(sum(value));
https://jsfiddle.net/f7uqw7cL/

How do I rearrange an array with similar items so that similar items will never neighbor each other? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So for example if I have this array:
var input = [1,1,2,4,6,7,7,1];
I want the output to be something like:
[1,2,1,4,6,7,1,7]
The order of the new array does not matter, as long as similar items will never (or at least as seldom as possible) neighbor each other.
I can use plain JavaScript as well as underscore.js.
Try the following:
var input = [1,1,2,4,6,7,7,1];
input.sort()
var output = [];
var len = input.length;
for (var i = 0; i < Math.floor((len / 2)); i++) {
output.push(input[i]);
output.push(input[len - i - 1]);
}
if (len % 2) {
var left_over = input[Math.floor(len / 2)];
if (left_over == output[0]) {
output.push(left_over);
} else {
output.unshift(left_over);
}
}
Or see http://jsfiddle.net/d0j3Lfa3/1.
The solution sorts the numbers then alternates high and low. It deals with an odd number of elements, including corner cases such as [1,1,2] and [1,2,2] where it needs to push the middle element differently to pass. Since the input is sorted, input order doesn't affect the output.
This answer may help simplify things a bit.

My first permutation lesson - what is this code missing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Translated (apparently wrongly) from a C++ book.
If I can get it to work, then I can start trying to understand it.
function recPermute(soFar, rest)
{
if (rest==="")
{
console.log(soFar);
}
else
{
for(i=0; i<rest.length; i++) // <<< error was in not declaring the "i"
{
var next = soFar + rest[i];
var remaining = rest.substr(0,i) + rest.substr(i+1);
recPermute(next, remaining);
}
}
}
function listPerm(s)
{
recPermute("",s);
}
listPerm("kitcap")
You need to declare i so it's scoped to recPermute:
for(var i=0; i<rest.length; i++)
Without the var, it'll be created as a global so each call to recPermute will alter it for any other calls.
for JavaScript, use charAt(), instead of using array like acessing.
var next = soFar + rest.charAt(i);
One thing that could be an issue is you are using effectively the same i for each call to the function. You need to declare a local i or it will be declared in the global scope.
for(var i = 0; ....

how can I call an unnamed instance in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So if I make a class, and then create new instances of that class without naming them -maybe with a loop that creates a bunch of instances- how can I call a specific (or unspecific) instance? For example, if I'm generating a bunch a squares, but I want to move a specific one somewhere, how would I do that?
Sorry if this is a total noob question, or if I miss-used some terminology, but I'm pretty new to programming.
Example code:
function example(x){
this.x = x;
}
for(var i=0; i<10; i++){
new example(1);
}
//now how would I get a specific instance of examples to have x = say, 10.
You could put each square in an array and access them that way:
function Square(i){
this.index = i;
}
Square.prototype = {
constructor: Square,
intro: function(){
console.log("I'm square number "+this.index);
}
}
var squares = [];
for(var i = 0;i < 10;i++){
squares.push(new Square(i));
}
squares.forEach(function(square){
// do something with each square
square.intro();
});
Demo: http://jsfiddle.net/louisbros/MpcrT/1/

Categories

Resources