Consider the following multiple choice question:
What's the color of apples?
a. red
b. green
c. blue
d. black
Now, I want to calculate all it's possible answers and I could do it manually, like so...:
a
b
c
d
a, b
a, c
a, d
b, c
b, d
c, d
a, b, c
a, b, d
a, c, d
b, c, d
a, b, c, d
... but that's prone to human error. How could I do this programmatically with JavaScript?
My initial thought is to define the total number of choices (a,b,c,d = 4)...:
const TOTAL_CHOICES = 4;
// TO-DO
... but then I don't know what the next step should be. Any ideas?
With the help of #nenad-vracar's comment I found a solution:
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
var result = combinations('abcd').sort();
for (combination of result) {
document.body.innerHTML += combination + '<br>';
}
This will output:
a
ab
abc
abcd
abd
ac
acd
ad
b
bc
bcd
bd
c
cd
d
This implements the way I suggested in my comment.
var choices = ['d','c','b','a'];
var numCombos=Math.pow(2,choices.length);
var binNum;
writeln('Combos are:');
for (i=0;i<numCombos;i++)
{
binNum=(i.toString(2));
while (binNum.length<choices.length)
binNum='0'+binNum;
for (j=choices.length-1;j>=0;j--){
if (binNum[j]=='1') write(choices[j]);}
writeln();
}
Related
let a=4;
const b=5;
var c=6;
As per javaScript, what is the size of a, b, and c here?
For example:
function mf(z, x c, lol = b) { // I need lol = azazaz
let b = azazaz
...
}
Instead of lol = azazaz I obviously get b is not defined.
What I can do:
function mf(z, x, c, lol = "b") { //parameter lol is a string
let b = azazaz
lol = eval(lol) //now lol = azazaz
...
}
Also I can do this:
function mf(z, x, c, lol) { //parameter lol doesn't have default value
b = azazaz
if (lol == 0) {
lol = b //default value of lol = azazaz
}
...
}
But the first one looks so non-professional, the second one has extra if statement which I also don't want. Is there any better way to do this?
If defining the variable inside of the function isn't a hard requirement, you can probably leverage closures to do something like:
const b = 'azazaz';
function mf(z, x, c, lol = b) {
...
}
Or, perhaps use 'OR' to avoid if
function mf(z, x, c, lol) { //parameter lol doesn't have default value
let b = 'azazaz';
lol = lol || b;
...
}
If you need to change paramater and re-use it you must re-call the function like:
//every time function change lol and recall function
function mf(z, x, c, lol) {
console.log('lol now is: ' + lol);
lol++;
if (lol <= 10) {
mf(1, 2, 3, lol);
}
}
mf(1, 2, 3, 0);
I don't have a control over the function invocation and I need to pass a few parameters with a pointer. Is it possible to do it?
var a = function(a, b){
return a + b;
}
b = a; //parameter here???
Any help will be appreciated!
You can use currying, the outer function call will return a pointer to another function that references the parameters you passed. Then you can call the inner function without passing the parameters.
var a = function(x, y) {
function(){
return x + y;
}
}
b = a(2, 3);
b();
The correct way to pass parameters to a is to replace your last line with : b = a(1, 2);.
This will give you 3 as a result, stored into the variable b.
You can see the current value of b at any time by running console.log(b); :
var a = function(a, b){
return a + b;
}
b = a(1, 2); // parameters here
console.log(b); // prints 3
But as I said, the use of b twice here is confusing. A better naming would be :
// sum is a better name
var sum = function(a, b){
return a + b;
}
result = sum(1, 2); // parameters here : 1 corresponds to a, 2 to b
console.log(result); // prints 3
Figured out myself. I am sorry if the question is not clear enough.
This is what I needed.
var a = function(a, b) {
return function(){
console.log(a + b);
}
};
b = a(3, 4);
b();
Why is my code not working?
Chrome gives me the following error: Uncaught TypeError: Cannot read property 'toString' of undefined.
It works with 1,2,3,4,6,7,8,9 but does not work with 5,10,15,...
Please help me out.
Here is my javascript code:
<code><script>
function mmCal(val) {
var a, b, c, d, e, f, g, h, i;
a = val * 25.4;
b = a.toString().split(".")[0];
c = a.toString().split(".")[1];
d = c.toString().substr(0, 1);
e = +b + +1;
f = b;
if (d>5) {
document.getElementById("txtt").value = e;
} else {
document.getElementById("txtt").value = f;
}
}
</script></code>
Here is my html:
<code><input type="text" id="txt" value="" onchange="mmCal(this.value)"></code>
<code><input type="text" id="txtt" value=""></code>
As Sebnukem says
It doesn't work when a is an integer because there's no period to
split your string, and that happens with multiples of 5.
But you could have a trick so use a % 1 != 0 to know wherther the value is a decimal see the code below:
function mmCal(val) {
var a, b, c, d, e, f, g, h, i;
a = val * 25.4;
if(a % 1 != 0){
b = a.toString().split(".")[0];
c = a.toString().split(".")[1];
}
else{
b = a.toString();
c = a.toString();
}
d = c.toString().substr(0, 1);
e = +b + +1;
f = b;
if (d>5) {
document.getElementById("txtt").value = e;
} else {
document.getElementById("txtt").value = f;
}
}
That could you help you.
LIVE DEMO
It doesn't work when a is an integer because there's no period to split your string, and that happens with multiples of 5.
Strange way of rounding a number to an integer :-)
You are converting inches to millimeters, and then rounding that to an integer, right?
Why not use 'toFixed()' on the number? See: Number.prototype.toFixed()
I mean:
function mmCal(val) {
var a, rounded;
a = val * 25.4;
rounded = a.toFixed();
document.getElementById("txtt").value = rounded;
}
(you may also use "toFixed(0)" for the explicit precision).
Let's say i have three variables:
a, b, c
and i set them such values:
2,1,3
I have such string:
ilovemama
how could i change char position, via block's of three, in my case i have three blocks:
ilo
vem
ama
let's try on first block:
1 2 3
i l o
and i must change this position's via my a,b,c:
2 1 3
l i o
And so over, and then concat this block's into one line...
I think that i explain it normally.
I can do this on jQuery, but i can't imagine, how to do this on pure JS. i try a little bit, but this is without sense(
var string = 'some string'
a = string.charAt(0),
b = string.charAt(1),
c = string.charAt(2); // and so on
var newString = b + a + c; //oms
var otherString = c + b + a; //mos
.charAt(0) will select the first leter of the string(the one with index 0) and so on.
assigning the values to vars you can manipulate the string as I understand you want to do
for blocks,
doing;
var string='some string';
var a = string.slice(0, 3),
b = string.slice(3, 7),
c = string.slice(7, 11); and so on
Then the same
var newString = c +a +b; // will be = 'ringsome st'
To find an Index as you request in the comment you can use;
var str = "Hello There",
indexOfr = str.indexOf("r");
console.log(indexOfr); // outputs 9
A function could be;
function dynamo(string) {
var len = string.length-1,
parts = 3,
whereToCut = Math.floor(len/parts);
var a = string.slice(0, whereToCut),
b = string.slice(whereToCut, (whereToCut *2)),
c = string.slice((whereToCut *2), len+1);
return b + a + c;
//(or you could hwere some code to see what order you want, i dont understand your request there)
}
dynamo('what do you really want to do??');
//returns "u really wwhat do yoant to do??"