alter the direction of loop dynamically - javascript

I am trying to come up with an algorithm for an "approaching" behavior between two integers. Basically, given two integers, a and b, i want a to "approach" b, even if b is less than a. The way i think this should look is a swapping of the loop incrementer function:
for (var i = a; approachCond(i, a, b); approachDir(i,a, b)) {
// some fn(a, b);
}
where
approachCond(i, a, b) {
return a < b ? i < b : i > b;
}
and
approachDir(i, a, b) {
return a < b ? i++ : i--
}
However, when i try doing this the browser freezes (Chrome). Does anyone know how to dynamically alter the direction of a loop?

I think it's a little clearer to read if you just use a while loop:
'use strict';
let a = 12, b = 6;
let i = a;
while (i !== b) {
console.log(i);
i += a < b ? 1 : -1;
}
I even left the cute ternary since people seem so opposed to if-statements these days.

Your browser freezes because you're not altering the correct i. You're only manipulating the i that is in the approachDir function. If you return it & set the for scope i to the new value, it will work.
Try:
for (var i = a; approachCond(i, a, b); i = approachDir(i,a, b)) {
// some fn(a, b);
}
approachDir(i, a, b) {
return a < b ? i + 1 : i - 1
}

It seems as though you are overcomplicating something that is not that hard. You can just set the step to positive or negative. e.g.
var a = 20;
var b = 5;
for (var step = a > b ? -1 : +1; a != b; a += step)
{
console.log(a);
}

The problem is in approachDir. i++ and i-- are post-increment and post-decrement. That means they update the variable after they return its original value. So the function is returning the original value, not the updated one. To update the variable before returning, you should use ++i or --i.
But you don't need to use an increment operator at all, since the local variable is going away immediately. Just return the new value.
function approachDir(i, a, b) {
return a < b ? i + 1 : i - 1;
}
You also need to reassign the variable in the loop:
for (var i = a; approachCond(i, a, b); i = approachDir(i, a, b)) {
...
}
The way you wrote your code, you assumed that variables are passed by reference, not by value, so that the increment in the function would modify the caller's variable.

One option is to change approachDir to return a positive or negative value based on if a>b. The middle statement can include an || and work either way.
function approachDir(a, b){
return a>b?-1:1;
}
var a=3;
var b=1;
for (var i = a; i<b||i>b; i+=approachDir(a, b)) {
console.log(i);
}
a=1;
b=3;
for (var i = a; i<b||i>b; i+=approachDir(a, b)) {
console.log(i);
}

I made a slight change to #mathletics answer:
function loopDynamic(from, to) {
const direction = from < to ? 1 : -1;
let i = from
while (i!== to) {
// do whatever with i
i += direction;
}
}

Related

How to store data into variable.?

var a=1800, b=10;
if (a == b) {
document.write(a - b) = c;
}
else if (a > b) {
document.write(a - b) = c;
}
else {
document.write("Everything is wrong.") = c;
}
var x = c * 100;
document.write(x);
Hello friends, Can i store result of variable into "c". if yes then why i am not able to use the data for arithmetic calculations further.
I am getting 1790 as answer from if else statement.
The variable should be on the left side of the equals sign. document.write doesn't return a value so you should do the assignment before that line.
else if (a > b) {
c = a - b;
document.write(c);
}
That isn't really even valid JavaScript.
You're calling a function (document.write()) and then using the assignment operator on it (which you can't do).
The end results would be equivalent of writing something like undefined = 7 since JavaScript will evaluated/execute the function first.
C is also never declared anywhere so you'll have a problem with that potentially as well.
Instead you'll want to do something like this:
let c; //declare C but don't assign it a value
const a = 1800;
const b = 10;
if(a === b || a > b){ //Since you're doing the same thing combine the conditions
c = a - b;
document.write(c);
} else {
document.write("Somethings wrong")
}
let x = c * 100; // If c is undefined you'll get NaN similar to above, otherwise you'll get a result
document.write(x);
Firstly you should initialize your variables, then else if statement doesn't make any sense because in if your are doing the same thing which you could do with || OR operator.
const a = 1800;
const b = 10;
let c = null;
if (a == b || a > b) {
c = (a - b) * 100;
} else {
c = "Everything is wrong.";
}
document.write(c);
Document.write does not return the result of the equation, and your assignments are incorrect. When assigning variables think of it this way:
"I have a variable C. I would like C to store the value of Y."
So C = Y. It's backwards from the way math does it. ( Equation = result. ) In programming, it tends to be StorageLocation = Equation.
Why do I say tends to be? There's got to be a language out there that doesn't hold up to that paradigm!
Here's your updated code:
var a=1800, b=10, c = 0; // Initializing c for document.write is a good practice.
if (a == b) {
c = a-b;
}
else if (a > b) {
c = a-b; /* As the other two posters noticed ... this does the same thing as the a == b. I am assuming you'd like to do something a little different with the two blocks. */
}
else {
c = "Everything is wrong.";
}
document.write(c); // "Don't Repeat Yourself" or "DRY" is good practice.
var x = c * 100; // Note - Multiplying your string by 100 is weird.
document.write(x);

How do I convert to binary in javascript? How do I have a recursive function return data from each call all at once?

Header: I am console.logging what I want to return.
I want to have a data structure store the necessary powers of 2 that I am calculating, but because it's recursive, I'm having trouble saving the output in a string or array without the data structure being overwritten each function call.
It's almost like I want to use request.session. How do I store what I'm console logging?
I tried:
if(!this) {then that}
But this is never there so it runs every time. Maybe passing it through the parameter is a way to solve this but the parameter is only n, and I want the function to only take in n and then output some information relevant to the binary equivalent, but I am probably going about it all wrong.
I think it's interesting that I can console.log it but I can't push it to an array.
How can I proceed?
function binary(n){
if(n == 0){
console.log("The end");
return 0;
}
var b, x;
for(var i = 0; i <= 10; i++){
b = Math.pow(2,i);
if(b > n){
var p = i-1;
console.log("2 to the",p, "is on");
x = Math.pow(2,i-1);
n = n-x;
return binary(n);
}
}
}
binary(12);
binary(365);
// 12 == 2^3(1) + 2^2(1) + 2^1(0) + 2^0(0)
// 12 == 1100
Footer:
[Running] node "/Users/maxwelljann/Desktop/cracking_the_coding_interview/b3.js"
2 to the 3 is on
2 to the 2 is on
The end
2 to the 8 is on
2 to the 6 is on
2 to the 5 is on
2 to the 3 is on
2 to the 2 is on
2 to the 0 is on
The end
Just because a function has a formal parameter doesn't mean you have to pass it in the initial call. Any parameters you don't fill get set to undefined:
function showMeTheArray (num, array) {
console.log(array);
}
showMeTheArray(42);
And with ES2015, you can set default parameters for undefined arguments:
function showMeTheArray (num, array = [1, 2, 3]) {
console.log(array);
}
showMeTheArray(42);
showMeTheArray(42, ['I passed it this time']);
So you can pass an array in your recursive calls to binary(), but not require it from the user:
function binary (n, array = []) {
if (n == 0) {
return [...array, 'The end'];
}
var b, x;
for (var i = 0; i <= 10; i++) {
b = Math.pow(2, i);
if (b > n) {
var p = i - 1;
const newArray = [...array, `2 to the ${p} is on`];
x = Math.pow(2, i - 1);
n = n - x;
return binary(n, newArray);
}
}
}
console.log(binary(12));
console.log(binary(365));
I can think of a global array to store whatever you are console logging.
var globalArray = []; //Global array
function binary(n){
if(n == 0){
console.log("The end");
return 0;
}
var b, x;
for(var i = 0; i <= 10; i++){
b = Math.pow(2,i);
if(b > n){
var p = i-1;
globalArray.push("2 to the",p, "is on"); //Add elements to the global array
x = Math.pow(2,i-1);
n = n-x;
return binary(n);
}
}
}
Output: The entries don't get overwritten since the variable is outside the function scope.
binary(2)
VM458:5 The end
0
globalArray
(3) ["2 to the", 1, "is on"]
binary(3)
VM458:5 The end
0
globalArray
(9) ["2 to the", 1, "is on", "2 to the", 1, "is on", "2 to the", 0, "is on"]
In general it is not great practice to use global variables, but if it just for experimenting, then well and good. check out this article for more info.

How do I add two numbers in JavaScript "without using `+` or `-` operators"?

I know an alternative to using the + sign for addition is to do something like this:
int add(int a, int b)
{
if(b == 0)
return sum;
sum = a ^ b;
carry = (a & b) << 1;
return add(sum,carry);
}
But I have two problems:
This is C++, not JavaScript. Is this supported in JavaScript?
It's obvious the whole trick is in ^ & <<, but I don't know how to start looking for them in JavaScript, because I don't know what they are called.
What should I be googling for even?
I tried to write this in JavaScript ... but seems I miss something
var getSum = function(a, b) {
return (a ^ b, (a & b) << 1)
};
We will use bitwise operators and will use recursion.
We use this method when we have a few low resources. Read more about when to use this method!
var getSum = function(a, b) {
if (b == 0) {
return a;
} else {
return getSum(a ^ b, (a & b) << 1)
}
};
ECMAScript 6 one-liner solution as suggested by #PatrickRoberts:
const getSum = (a,b) => b ? getSum(a ^ b, (a & b) << 1) : a;
Another solutions:
2- Arrays technique Array.prototype.fill()
const getSum = (a, b) => {
const firstArr = new Array(a).fill(true);
const secondArr = new Array(b).fill(true);
return firstArr.concat(secondArr).length
}
3- workaround to use plus sign without writing it:
const getSum = (a, b) => eval(''.concat(a).concat(String.fromCharCode(0x2B)).concat(b));
Well ok i am answering to the question as clearly described in the header. No + and no - operations right..? Yet... not with bitwise but with pure math should be a valid answer i suppose.
var x = 1,
y = 2,
sum = Math.log2(2**x * 2**y);
console.log(sum);
const add = (a, b) => new Function('a', 'b', `return ${a} ${String.fromCharCode(43)} ${b}`)(a, b);
We can implement same using while loop. We have to shift the carry to left and add it to binary sum of numbers till there will no carry. (As we follows the practice in addition of decimals.)
function getSum(a, b){
while(b!=0){
var carry = a&b; //calculate if is there any carry we need to add
a = a^b; // a is used to hold the sum
b = carry<<1; //b is used to hold left shift carry
}
return a;
}
document.write(getSum(7, 5))
It's possible to use arrays structures to perform a sum operation.
function getSum(a, b){
return Array(a).concat(Array(b)).length / 100;
}
Each input is coerced to an array, for instance, an input of value 5 would be coerced to an array of 5 elements. After coercing both inputs, the arrays are joined into a single array. The length of the final array is returned, by dividing to 100 to deal with the sum of decimal values.
Now, let's try to be defensive about invalid input cases, such as strings or falsy values.
const DEFAULT_NUMBER_VALUE = 0;
const DEFAULT_PRECISION = 100;
function parseAddInput(input){
if (!input) {
return DEFAULT_NUMBER_VALUE;
}
if (typeof input === 'string'){
input = parseInt(input);
}
const roundedNumber = Math.round(input * (10 * DEFAULT_PRECISION));
return roundedNumber;
}
function getSum(a, b){
return Array(
parseAddInput(a)
).concat(
Array(parseAddInput(b))
).length / 100;
}
function add(number1, number2){
return getSum(number1, number2);
}
The same approach as #PatrickRoberts suggested, but without recursion:
const add = (a, b) => {
let c;
while (a !== 0) {
c = b & a;
b = b ^ a;
c = c << 1;
a = c;
}
return b;
};

javascript - where should I put "var" in order to get specifc values

That's a code fragment task - you should enter "var" (as many as want) in it in order to get 17 in the first, and 21 in the second alert. I thing that I have met this before, but still was not able to solve the issue.
a = 3;
b = 2;
function line(x) {
a = 5;
b = 4;
return a*x + b
}
//b should be 17
b = line( a ) - b;
alert( b );
//c should be 21
c = line ( a ) + b;
alert(c);
If you put "var" in the function in front of b, it will alert "17". The next alert gives us 46 because of the new value of b, return by the function.
function line(x) {
a = 5;
var b = 4;
return a*x + b
}
That's the source of the task:
http://www.codecademy.com/courses/javascript-for-jquery/1?curriculum_id=4fc3018f74258b0003001f0f/#!/exercises/3
Using exactly what's given, in exactly the way it's given is impossible.
What I mean by that is if the call:
c = line(a) + b;
is dependent upon the value of b which is the assignment at:
b = line(a) - b;
Then it's 100% impossible to either have made a a significantly-small number, or made b a significantly-large negative number to make the math work.
Therefore it's my belief that they're intended to be two separate checks.
Best-case scenario, if we're trying to have b=17 included:
a = 3;
3 * 5 = 15 + 4 = 19 + 4 = 23;
That's the smallest you're going to get, assuming you run the two back-to-back.
Even if you did it that way, you wouldn't get b = line(a) - b = 17 on the first run...
If it was written:
c = line(a) - b;
d = line(a) + b;
Then you could run both in succession and get the expected result.
Or you can run:
var a = 3,
b = 2;
function line (x) {
var a = 5,
b = 4;
return a*x + b;
}
b = line(a) - b;
and get 17.
Then you can run:
var a = 3,
b = 2;
function line (x) {
var a = 5,
b = 4;
return a*x + b;
}
c = line(a) + b;
(ie: the exact same setup with a different instigator, and without the saved b value from the return of the previous call), and get the desired result.
But it's not possible to run both of them one after the other, and expect to have them both work, without doing anything to the code but add a var or four.
Keep your function like this, if you want to maintain consisitency. Using "var" before a and b will make them local to the function block and that call. Otherwise they will refer to the global variable.
function line(x) {
var a = 5;
var b = 4;
return a*x + b
}

How can I make var a = add(2)(3); //5 work?

I want to make this syntax possible:
var a = add(2)(3); //5
based on what I read at http://dmitry.baranovskiy.com/post/31797647
I've got no clue how to make it possible.
You need add to be a function that takes an argument and returns a function that takes an argument that adds the argument to add and itself.
var add = function(x) {
return function(y) { return x + y; };
}
function add(x) {
return function(y) {
return x + y;
};
}
Ah, the beauty of JavaScript
This syntax is pretty neat as well
function add(x) {
return function(y) {
if (typeof y !== 'undefined') {
x = x + y;
return arguments.callee;
} else {
return x;
}
};
}
add(1)(2)(3)(); //6
add(1)(1)(1)(1)(1)(1)(); //6
It's about JS curring and a little strict with valueOf:
function add(n){
var addNext = function(x) {
return add(n + x);
};
addNext.valueOf = function() {
return n;
};
return addNext;
}
console.log(add(1)(2)(3)==6);//true
console.log(add(1)(2)(3)(4)==10);//true
It works like a charm with an unlimited adding chain!!
function add(x){
return function(y){
return x+y
}
}
First-class functions and closures do the job.
function add(n) {
sum = n;
const proxy = new Proxy(function a () {}, {
get (obj, key) {
return () => sum;
},
apply (receiver, ...args) {
sum += args[1][0];
return proxy;
},
});
return proxy
}
Works for everything and doesn't need the final () at the end of the function like some other solutions.
console.log(add(1)(2)(3)(10)); // 16
console.log(add(10)(10)); // 20
try this will help you in two ways add(2)(3) and add(2,3)
1.)
function add(a){ return function (b){return a+b;} }
add(2)(3) // 5
2.)
function add(a,b){
var ddd = function (b){return a+b;};
if(typeof b =='undefined'){
return ddd;
}else{
return ddd(b);
}
}
add(2)(3) // 5
add(2,3) // 5
ES6 syntax makes this nice and simple:
const add = (a, b) => a + b;
console.log(add(2, 5));
// output: 7
const add2 = a => b => a + b;
console.log(add2(2)(5));
// output: 7
Arrow functions undoubtedly make it pretty simple to get the required result:
const Sum = a => b => b ? Sum( a + b ) : a;
console.log(Sum(3)(4)(2)(5)()); //14
console.log(Sum(3)(4)(1)()); //8
This is a generalized solution which will solve add(2,3)(), add(2)(3)() or any combination like add(2,1,3)(1)(1)(2,3)(4)(4,1,1)(). Please note that few security checks are not done and it can be optimized further.
function add() {
var total = 0;
function sum(){
if( arguments.length ){
var arr = Array.prototype.slice.call(arguments).sort();
total = total + arrayAdder(arr);
return sum;
}
else{
return total;
}
}
if(arguments.length) {
var arr1 = Array.prototype.slice.call(arguments).sort();
var mytotal = arrayAdder(arr1);
return sum(mytotal);
}else{
return sum();
}
function arrayAdder(arr){
var x = 0;
for (var i = 0; i < arr.length; i++) {
x = x + arr[i];
};
return x;
}
}
add(2,3)(1)(1)(1,2,3)();
This will handle both
add(2,3) // 5
or
add(2)(3) // 5
This is an ES6 curry example...
const add = (a, b) => (b || b === 0) ? a + b : (b) => a + b;
This is concept of currying in JS.
Solution for your question is:
function add(a) {
return function(b) {
return a + b;
};
}
This can be also achieved using arrow function:
let add = a => b => a + b;
solution for add(1)(2)(5)(4)........(n)(); Using Recursion
function add(a) {
return function(b){
return b ? add(a + b) : a;
}
}
Using ES6 Arrow function Syntax:
let add = a => b => b ? add(a + b) : a;
in addition to what's already said, here's a solution with generic currying (based on http://github.com/sstephenson/prototype/blob/master/src/lang/function.js#L180)
Function.prototype.curry = function() {
if (!arguments.length) return this;
var __method = this, args = [].slice.call(arguments, 0);
return function() {
return __method.apply(this, [].concat(
[].slice.call(args, 0),
[].slice.call(arguments, 0)));
}
}
add = function(x) {
return (function (x, y) { return x + y }).curry(x)
}
console.log(add(2)(3))
Concept of CLOSURES can be used in this case.
The function "add" returns another function. The function being returned can access the variable in the parent scope (in this case variable a).
function add(a){
return function(b){
console.log(a + b);
}
}
add(2)(3);
Here is a link to understand closures http://www.w3schools.com/js/js_function_closures.asp
const add = a => b => b ? add(a+b) : a;
console.log(add(1)(2)(3)());
Or (`${a} ${b}`) for strings.
With ES6 spread ... operator and .reduce function. With that variant you will get chaining syntax but last call () is required here because function is always returned:
function add(...args) {
if (!args.length) return 0;
const result = args.reduce((accumulator, value) => accumulator + value, 0);
const sum = (...innerArgs) => {
if (innerArgs.length === 0) return result;
return add(...args, ...innerArgs);
};
return sum;
}
// it's just for fiddle output
document.getElementById('output').innerHTML = `
<br><br>add() === 0: ${add() === 0 ? 'true' : 'false, res=' + add()}
<br><br>add(1)(2)() === 3: ${add(1)(2)() === 3 ? 'true' : 'false, res=' + add(1)(2)()}
<br><br>add(1,2)() === 3: ${add(1,2)() === 3 ? 'true' : 'false, res=' + add(1,2)()}
<br><br>add(1)(1,1)() === 3: ${add(1)(1,1)() === 3 ? 'true' : 'false, res=' + add(1)(1,1)()}
<br><br>add(2,3)(1)(1)(1,2,3)() === 13: ${add(2,3)(1)(1)(1,2,3)() === 13 ? 'true' : 'false, res=' + add(2,3)(1)(1)(1,2,3)()}
`;
<div id='output'></div>
can try this also:
let sum = a => b => b ? sum(a + b) :a
console.log(sum(10)(20)(1)(32)()) //63
const sum = function (...a) {
const getSum = d => {
return d.reduce((i,j)=> i+j, 0);
};
a = getSum(a);
return function (...b) {
if (b.length) {
return sum(a + getSum(b));
}
return a;
}
};
console.log(sum(1)(2)(3)(4,5)(6)(8)())
function add(a, b){
return a && b ? a+b : function(c){return a+c;}
}
console.log(add(2, 3));
console.log(add(2)(3));
This question has motivated so many answers already that my "two pennies worth" will surely not spoil things.
I was amazed by the multitude of approaches and variations that I tried to put "my favourite" features, i. e. the ones that I would like to find in such a currying function together, using some ES6 notation:
const add=(...n)=>{
const vsum=(a,c)=>a+c;
n=n.reduce(vsum,0);
const fn=(...x)=>add(n+x.reduce(vsum,0));
fn.toString=()=>n;
return fn;
}
let w=add(2,1); // = 3
console.log(w()) // 3
console.log(w); // 3
console.log(w(6)(2,3)(4)); // 18
console.log(w(5,3)); // 11
console.log(add(2)-1); // 1
console.log(add()); // 0
console.log(add(5,7,9)(w)); // 24
.as-console-wrapper {max-height:100% !important; top:0%}
Basically, nothing in this recursively programmed function is new. But it does work with all possible combinations of arguments mentioned in any of the answers above and won't need an "empty arguments list" at the end.
You can use as many arguments in as many currying levels you want and the result will be another function that can be reused for the same purpose. I used a little "trick" to also get a numeric value "at the same time": I redefined the .toString() function of the inner function fn! This method will be called by Javascript whenever the function is used without an arguments list and "some value is expected". Technically it is a "hack" as it will not return a string but a number, but it will work in a way that is in most cases the "desired" way. Give it a spin!
Simple Recursion Solution for following use cases
add(); // 0
add(1)(2)(); //3
add(1)(2)(3)(); //6
function add(v1, sum = 0) {
if (!v1) return sum;
sum += v1
return (v2) => add(v2, sum);
}
function add() {
var sum = 0;
function add() {
for (var i=0; i<arguments.length; i++) {
sum += Number(arguments[i]);
}
return add;
}
add.valueOf = function valueOf(){
return parseInt(sum);
};
return add.apply(null,arguments);
}
// ...
console.log(add() + 0); // 0
console.log(add(1) + 0);/* // 1
console.log(add(1,2) + 0); // 3
function A(a){
return function B(b){
return a+b;
}
}
I found a nice explanation for this type of method. It is known as Syntax of Closures
please refer this link
Syntax of Closures
Simply we can write a function like this
function sum(x){
return function(y){
return function(z){
return x+y+z;
}
}
}
sum(2)(3)(4)//Output->9
Don't be complicated.
var add = (a)=>(b)=> b ? add(a+b) : a;
console.log(add(2)(3)()); // Output:5
it will work in the latest javascript (ES6), this is a recursion function.
Here we use concept of closure where all the functions called inside main function iter refer and udpate x as they have closure over it. no matter how long the loop goes , till last function , have access to x.
function iter(x){
return function innfunc(y){
//if y is not undefined
if(y){
//closure over ancestor's x
x = y+x;
return innfunc;
}
else{
//closure over ancestor's x
return x;
}
}
}
iter(2)(3)(4)() //9
iter(1)(3)(4)(5)() //13
let multi = (a)=>{
return (b)=>{
return (c)=>{
return a*b*c
}
}
}
multi (2)(3)(4) //24
let multi = (a)=> (b)=> (c)=> a*b*c;
multi (2)(3)(4) //24
we can do this work using closure.
function add(param1){
return function add1(param2){
return param2 = param1 + param2;
}
}
console.log(add(2)(3));//5
I came up with nice solution with closure, inner function have access to parent function's parameter access and store in its lexical scope, when ever we execute it, will get answer
const Sum = function (a) {
return function (b) {
return b ? Sum(a + b) : a;
}
};
Sum(1)(2)(3)(4)(5)(6)(7)() // result is 28
Sum(3)(4)(5)() // result is 12
Sum(12)(10)(20) // result is 42
enter image description here
You should go in for currying to call the function in the above format.
Ideally, a function which adds two numbers will be like,
let sum = function(a, b) {
return a + b;
}
The same function can be transformed as,
let sum = function(a) {
return function(b) {
return a+b;
}
}
console.log(sum(2)(3));
Let us understand how this works.
When you invoke sum(2), it returns
function(b) {
return 2 + b;
}
when the returned function is further invoked with 3, b takes the value 3. The result 5 is returned.
More Detailed Explanation:
let sum = function(a) {
return function(b) {
return a + b;
}
}
let func1 = sum(2);
console.log(func1);
let func2 = func1(3)
console.log(func2);
//the same result can be obtained in a single line
let func3 = sum(2)(3);
console.log(func3);
//try comparing the three functions and you will get more clarity.
This is a short solution:
const add = a => b => {
if(!b) return a;
return add(a + b);
}
add(1)(2)(3)() // 6
add(1)(2)(3)(4)(5)() // 15

Categories

Resources