I have this snippet that is is getting 3 numbers and its working distinguing them. If one of 3 numbers is diferent than the others it must return its correspondend.
An input example:
1 1 0
0 0 0
1 0 0
output must be:
C
*
A
The approach i had was that one:
var input = require('fs').readFileSync('stdin', 'utf8')
var lines = input.split('\n')
for (let i = 0; i < lines.length; i++) {
var round = lines[i].split(' ').map(i => parseInt(i))
// console.log(round); [1, 1, 0]
var A = round[0]
var B = round[1]
var C = round[2]
if(A === B && A === C){
console.log("*");
} else if (A === B && A !== C) {
console.log("C");
} else if (A !== B && A === C) {
console.log("B");
} else if (A !== B && A !== C) {
console.log("A");
}
}
I'm not sure what the problem is exactly but if it is to try to minimize the code in some way one thing to notice is that we don't care whether the the values are 0 or 1 only whether the players have chosen the same or not so once we have the values of A, B and C for a round we can just do:
(UPDATE: thanks to a comment from #Samathingamajig a redundant != comparison has been removed)
console.log( ((A==B)&&(B==C)) ? '*' : (A==B) ? 'C' : (A==C) ? 'B' : 'A' );
Like #Samathingamajig said:
function _0or1(arr) {
[A,B,C] = arr
if (A === B && A === C) {
console.log("*");
} else if (A === B) {
console.log("C");
} else if (A === C) {
console.log("B");
} else {
console.log("A");
}
}
_0or1([0,0,0])
_0or1([1,0,0])
_0or1([0,1,0])
_0or1([0,0,1])
_0or1([1,1,0])
_0or1([0,1,1])
_0or1([1,0,1])
_0or1([1,1,1])
I am facing a problem to create a loop which will loop back to the previous functions when the user does not want the program to stop (if the user wants it to stop, the program will continue with other functions).
I need to create a list of functions to do base conversion while showing the logic:
Step1: prompt for a number
Step2: prompt for an alphabet (b for Binary/o for Octal/h for Hexadecimal) as the base
Step3: convert it to a string (e.g. "108sup10 = 1101100sup2" & "63300268sup10 = 3C5E2A7sup16")
Step4: alert the string answer in a statement (e.g: Base 10 number 63300268 is 3C5E2A7 in Hexadecimal)
Step5: prompt to stop. If user's input is not "s", it will repeat step 1~4, else it continue to step 6.
Step 6: alert the max and min number entered from (repeated) step1's input.
for step 1,2,3,4,6, it is mandatory to use functions.
May I know how do I code for STEP5 in order to loop back from step 1-4 when stopping is prompted? Do I need a function for this?
//prompt to get number
function getNumber() {
var myNumber;
do {
myNumber = Number(prompt("Enter an unsigned base 10 number:")); //prompt user's input to be excecuted first
} while (myNumber < 0) //loop will run again and again as long as the number is less than zero
return myNumber;
}
//prompt to get base
function getBase() {
var myBase;
do {
myBase = (prompt("Enter b for binary, o for octal and h for hexadecimal"));
} while (!(myBase == "b" || myBase == "B" || myBase == "s" || myBase == "S"|| myBase == "h" || myBase == "H")) //loop if the input is not b, s or h
return myBase;
}
//converting the base to the number
function baseConversion(number, newBase) {
var arr = [];
if (newBase == "b" || newBase == "B") {
newBase = 2;
} else if (newBase == "o" || newBase == "O") {
newBase = 8;
}else if (newBase == "h" || newBase == "H") {
newBase = 16;
}
do { //putting the each remainder at the front of the array
arr.unshift(number%newBase);
number = Math.floor(number/newBase); //round down the divided answer
} while (number>newBase-1) //loop as long as this condition holds
arr.unshift(number);
return arr;
}
//function to string the arrays
function convertToString(number, base) {
var resultString = ""
for (var i = 0; i < results.length; i++) {
var digit = results[i];
if (digit > 9) {
switch (digit) {
case 10:
digit = 'A'
break;
case 11:
digit = 'B'
break;
case 12:
digit = 'C'
break;
case 13:
digit = 'D'
break;
case 14:
digit = 'E'
break;
case 15:
digit = 'F'
break;
}
}
resultString += digit;
}
return resultString
}
//function to alert the answer statement
function alertAnswer() {
var statement = alert("Base 10 number:" + myNumber + "is" + base + "in" + myBase);
return statement;
}
//function to find the maximum number in the array
function myMax(myArray) {
var max = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > max) {
max = myArray[z];
}
}
return max;
}
//function to find the minimum number in the array
function myMin(myArray) {
var min = myArray[0];
for (var z = 0; z < myArray.length; z++) {
if (myArray[z] > min) {
min = myArray[z];
}
}
return min;
}
Sorry if I'm mistaken, but is this what you're looking for?
var promptVar = false;
do
{
//function calls for steps 1~4
prompt();
}
while (promptVar == false)
function prompt()
{
if (confirm('Do you want to continue to step 6?'))
{
promptVar = true;
} else {
promptVar = false;
}
}
I've writen a function in JavaScript that checks through all the combinations of three digits between 1 and 9 and gives me the number of combinations that follow this pattern
√(x^2 + y^2 + z^2) = a natural number (a full number like 24 or 34 but not 2.54)
√ = square root , ^2 = to the power of 2,
My problem is that whenever I run the function the computer gets stuck and the function never ends so it doesn't return an answer.
I would very much appreciate if someone could tell me whats wrong with it
(I'm running my programs on the chrome browser console)
function mmd() {
var chk = false;
var a = 1;
var b = 1;
var c = 1;
var d = 1;
var e = 0;
while(chk != true) {
d = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2)+Math.pow(c, 2));
if( d == d.toFixed(0)) {
e++;
}
else {
if((b == 9) && (a == 9) && (c == 9)) {chk = true;}
else if((a == 9) && (b == 9)) {c++;}
else if(b == 9) {b = 1; a++;}
else if(c == 9) {c = 1; b++;}
else if(c < 9) {c++;}
}
}
return e
}
This part of the code is causing it to never end:
if (d == d.toFixed(0)){} else {}
If the result of the formula is an integer, you add 1 to e, but you don't increment the other variables, because of the else. It keeps doing e++ for ever. So you need to remove that else.
I also took the liberty or removing that chk variable, and instead used while(true), which will be ended by a return of the final result:
function mmd() {
var a = 1, b = 1, c = 1, d, e = 0;
while(true) {
d = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2)+Math.pow(c, 2));
if( d == parseInt(d, 10)) {
e++;
}
if((b == 9) && (a == 9) && (c == 9)) {return e;}
else if((a == 9) && (b == 9)) {c++;}
else if(b == 9) {b = 1; a++;}
else if(c == 9) {c = 1; b++;}
else {c++;}
}
}
alert(mmd());
It gets stuck once it hits the e++ block and never increases a, b, or c.
function mmd()
{
var keepGoing = true;
var a = 1, b = 1, c = 1, d, e = 0;
while(keepGoing)
{
// calculate d
d = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) + Math.pow(c, 2));
// check if it is a whole number
if(d == d.toFixed(0)) e++;
// if we're done then stop
if(a == 9 && b == 9 && c == 9){ keepGoing = false; }
// if c is less than 9 then increase it
else if(c < 9){ c++; }
// if c is 9 and b is less than 9 then set c back to 1 and increase b
else if(b < 9){ c = 1; b++; }
// if c is 9 and b is 9 then set both back to 1 and increase a
else if(a < 9){ c = b = 1; a++; }
}
return e;
}
First I have an array, like this:
var arr = [[2,3,4,5,6,7,8,9,10],
[3,4,5,6,7,8,9,10,11],
[4,5,6,7,8,9,10,11,12]]
It is not necessarily should be this array, it could be any other, it is just example. So I need to know the count of all possible permutations, something like this:
2,3,4
2,3,5
...
2,3,12
2,4,5
2,4,6
...
2,4,12
...
10,11,12
It is not simple permutations, as I understand. And also I don't need all this combination, I just need their count, may be exist formula or something like this. In this example I know that answer is 165. Thank you
Assuming that the order must increase:
var arr = [[2,3,4,5,6,7,8,9,10],
[3,4,5,6,7,8,9,10,11],
[4,5,6,7,8,9,10,11,12]];
function count(stack, history) {
var counter = 0;
history = history || [];
stack[0].forEach(function(it) {
if (! history.length || history[history.length -1] < it) {
if (stack[1]) {
counter += count(stack.slice(1), history.concat([it]));
} else {
counter++;
}
}
});
return counter;
}
console.log(count(arr)); //165
This is an interesting mathematical problem. I think what you are referring to is combinations and not permutations. I place the javascript here first. The math follows below that.
<script type="text/javascript">
var arr = new Array();
arr[0] = new Array(2,3,4,5,6,7,8,9,10);
arr[1] = new Array(3,4,5,6,7,8,9,10,11);
arr[2] = new Array(4,5,6,7,8,9,10,11,12);
function countcombinations() {
var history = new Array();
for (var i=0;i<arr[0].length;i++) {
for (var j=0;j<arr[1].length;j++) {
for (var k=0;k<arr[2].length;k++) {
if (arr[0][i] == arr[1][j] || arr[0][i] == arr[2][k] || arr[1][j] == arr[2][k]) {
} else {
// If all are different, consider this set as candidate
var found = false;
history.forEach(function(entry) {
if (
// The various possible sequences for comparison
(arr[0][i] == entry[0] && arr[1][j] == entry[1] && arr[2][k] == entry[2]) ||
(arr[0][i] == entry[0] && arr[1][j] == entry[2] && arr[2][k] == entry[1]) ||
(arr[0][i] == entry[1] && arr[1][j] == entry[0] && arr[2][k] == entry[2]) ||
(arr[0][i] == entry[1] && arr[1][j] == entry[2] && arr[2][k] == entry[0]) ||
(arr[0][i] == entry[2] && arr[1][j] == entry[0] && arr[2][k] == entry[1]) ||
(arr[0][i] == entry[2] && arr[1][j] == entry[1] && arr[2][k] == entry[0])
) found = true;
});
// If not found, add to history
if (!found) history[history.length] = new Array(arr[0][i], arr[1][j], arr[2][k]);
}
}
}
}
alert ("Count: " + history.length);
}
countcombinations(); // Gives 165
</script>
Mathematically, the problem can be solved by iterating the first set and considering the possibilities for each. This is what the algorithm in the above javascript code does. The sequence of the elements do not matter. Each time a new unique combination is found, it appends this combination to the history. For fun, you could experiment with the above code and apply the math below to understand it further.
For a human approach to the math, let's consider the first set and use (X, Y, Z) to denote the three chosen numbers. The numbers in each set need to be in increasing order and the smallest number in subsequent sets also need to be in increasing order. The array given to us meets these conditions. So, for the first set:
[2,3,4,5,6,7,8,9,10]
When X = 2, i.e., (2, Y, Z), there are two possibilities to consider:
Y = 3
Y > 3
If Y = 3, then Z has 9 possibilities (4 through to 12).
If Y > 3, then if Z < 12, (Y, Z) has 8C2 (8 choose 2), or 28, possible combinations.
If Z = 12, then (Y, 12) has 8 possible combinations. Thus:
X = 2, Y = 3, Z > 3 : 9 combinations
X = 2, Y > 3, Z < 12 : 8C2 = 28 combinations
X = 2, Y > 3, Z = 12 : 8 combinations
In short, there are 45 possible combinations where any of the numbers is 2.
Phew! Moving on, from numbers 3 to 9, you can reuse two conditions above: whether Z < 12 or Z = 12. You will see a pattern emerging as shown below:
X = 3, Y > 3, Z < 12: 8C2 = 28 combinations
X = 3, Y > 3, Z = 12: 8 combinations
X = 4, Y > 4, Z < 12: 7C2 = 21 combinations
X = 4, Y > 4, Z < 12: 7 combinations
...
X = 9, Y > 9, Z < 12: 2C2 = 1 combination
X = 9, Y > 9, Z = 12: 2 combinations
Finally, when X = 10, there is only 1 possibility that is (10, 11, 12).
Thus the number of combinations (with a nice decreasing pattern) is:
X = 2: 9 + 8C2 + 8 = 45
X = 3: 8C2 + 8 = 36
X = 4: 7C2 + 7 = 28
X = 5: 6C2 + 6 = 21
X = 6: 5C2 + 5 = 15
X = 7: 4C2 + 4 = 10
X = 8: 3C2 + 3 = 6
X = 9: 2C2 + 2 = 3
X = 10: 1 = 1
TOTAL: 165
I am developing an application in titanium using Javascript. I need an open source implementation of encodeURIComponent in Javascript.
Can anybody guide me or show me some implementation?
The specification for this function is in 15.1.3.4.
Modern versions (2018) of V8 implement it in C++. See src/uri.h:
// ES6 section 18.2.6.5 encodeURIComponenet (uriComponent)
static MaybeHandle<String> EncodeUriComponent(Isolate* isolate,
Handle<String> component) {
which calls into Encode defined in uri.cc.
Older versions of V8 implemented it in JavaScript and distributed under the BSD license. See line 359 of src/uri.js.
// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
var unescapePredicate = function(cc) {
if (isAlphaNumeric(cc)) return true;
// !
if (cc == 33) return true;
// '()*
if (39 <= cc && cc <= 42) return true;
// -.
if (45 <= cc && cc <= 46) return true;
// _
if (cc == 95) return true;
// ~
if (cc == 126) return true;
return false;
};
var string = ToString(component);
return Encode(string, unescapePredicate);
}
It's not called encodeURIComponent there, but this code in the same file, esablishes the mapping:
InstallFunctions(global, DONT_ENUM, $Array(
"escape", URIEscape,
"unescape", URIUnescape,
"decodeURI", URIDecode,
"decodeURIComponent", URIDecodeComponent,
"encodeURI", URIEncode,
"encodeURIComponent", URIEncodeComponent
));
Here is my implementation:
var encodeURIComponent = function( str ) {
var hexDigits = '0123456789ABCDEF';
var ret = '';
for( var i=0; i<str.length; i++ ) {
var c = str.charCodeAt(i);
if( (c >= 48/*0*/ && c <= 57/*9*/) ||
(c >= 97/*a*/ && c <= 122/*z*/) ||
(c >= 65/*A*/ && c <= 90/*Z*/) ||
c == 45/*-*/ || c == 95/*_*/ || c == 46/*.*/ || c == 33/*!*/ || c == 126/*~*/ ||
c == 42/***/ || c == 92/*\\*/ || c == 40/*(*/ || c == 41/*)*/ ) {
ret += str[i];
}
else {
ret += '%';
ret += hexDigits[ (c & 0xF0) >> 4 ];
ret += hexDigits[ (c & 0x0F) ];
}
}
return ret;
};
What for do you need encodeuricomponent? It is already present in JS.
Anyway, here's an example of implementation:
http://phpjs.org/functions/rawurlencode:501#comment_93984