This question already has answers here:
JavaScript variable number of arguments to function
(12 answers)
Closed 11 months ago.
let sum = 0;
function innerAdd(a,b){
sum = a + b;
console.log(sum);
}
innerAdd(5,5,25,25) // Still getting 10 as output
Javascript only recognizes the first two values. if you want to be able to add more values you will have to add a for() cycle. something like this:
function sum() {
var total = 0;
for (var i = 0; i < arguments.length; i++) {
total += arguments[i];
}
alert(total);
}
sum(1,2,3,4);
this way you can add any number of parameters, sum(5,5,25,25) will be 60.
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
I want to optimize my javascript code with a FOR by i can't do this and i don't know why.
My code :
let pokemon1 = 'premier';
let pokemon2 = 'second';
let pokemon3 = 'troisieme';
for (var i = 1; i < 4; i++) {
console.log(pokemon[i]);
}
Do you know why it doesn't work ?
Thank you very much and sorry if i am a noob.
You should place the pokemon in an array:
let pokemon = [];
pokemon[0] = "premier";
pokemon[1] = "second";
pokemon[2] = "troisieme";
for(var i = 0; i < pokemon.length; i++){
console.log(pokemon[i])
};
Followed by some reading time: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
Since you are using a list, you should use [] to define an array :
let pokemons = ['premier', 'second', 'troisième'];
for (let i = 0; i < pokemons.length; i++) {
console.log(pokemons[i]);
}
See https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array for more information.
Also you should note that the first element of a list is 0.
So basically pokemons[0] === 'premier and pokemons[2] === 'troisième'
This question already has answers here:
Recursively print all permutations of a string (Javascript)
(11 answers)
Closed 6 years ago.
I need to write a JavaScript function that shows me all the permutations of the digits of the number passed as an argument, but not sure how to do it.
For example, given 1020, it would produce
1020 , 0120 , 0210 , 0102.
Here's a working solution. Hope it helps!
var result = [];
var newArray = [];
function permute(someArray) {
var i, ch;
for (i = 0; i < someArray.length; i++) {
ch = someArray.splice(i, 1)[0];
newArray.push(ch);
if (someArray.length == 0) {
result.push(newArray.slice());
}
permute(someArray);
someArray.splice(i, 0, ch);
newArray.pop();
}
return result;
};
var n = 1901;
var arr = (""+n).split("");
var myResult = permute(arr);
for(var i in myResult){
console.log(myResult[i].join(""));
}
This question already has answers here:
How can I shuffle an array? [duplicate]
(2 answers)
Closed 8 years ago.
My exact requirement is to return all numbers from 1 to 2000 randomly and shouldn't repeat a number twice. So we can say if i have
function generateRnNumber(){
var arr = [1,2,3,4,5,6,7,8,9,10,...2000];
randomNumber = Math.floor((Math.random()*2000));
return randomNUmber;
}
So if i call generateRnNumber a number between 1 - 2000 which is not returned before, or a unique number so if i call 2000 times i should get all the numbers but in random order. I don't want to keep an array with 2000 elements. Please help me in writing this function.
Keep an array with all possible values and randomize the order. When you need a random value, pop the array (removing the value the "possible values"-array).
To do this otherwise, you would anyway need an array to hold the "taken" values so i don't think you can get around keeping an array of the size of your "value range".
You can shuffle the array like this:
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
usage:
var myArray = [];
for(var i = 1; i <= 2000; i++) {
myArray.push(i);
}
var randomArray = shuffle(myArray).splice(0, 10); // an array with 10 random numbers varrying from 1-2000
source: How can I shuffle an array?
This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 8 years ago.
I'm like to count the total values in my array but I like to skip the same value
my array example, my real array will have about 1000 values.
json2=[aaa,aaa,aaa,aaa,bbb,bbb,bbb,ccc,ccc,ccc,ccc,ccc,ddd,ddd,ddd,eee,eee,fff];
and i want my count result to be
var countBoxID=6;
i only got
for(i in json2){
countBOXID ++
}
You can make use of this function:
function GetUnique(inputArray)
{
var outputArray = [];
for (var i = 0; i < inputArray.length; i++)
{
if ((jQuery.inArray(inputArray[i], outputArray)) == -1)
{
outputArray.push(inputArray[i]);
}
}
return outputArray;
}
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
I want to know why this procedure doesn't replace words
I have to do a procedure which reads a string and replace all word like this {{employee.Name}} into a value on the ticket's scope
var mySplitResult = Val.split(' ');
for (var i = 0; i < mySplitResult.length; i++) {
if (mySplitResult[i].match("{{") && mySplitResult[i].match(".")) {
var start = mySplitResult[i].lastIndexOf(".") + 1;
var end = mySplitResult[i].indexOf("}}");
var result = mySplitResult[i].substring(start, end);
for (var key in ticket.PNData) {
if (key == result) {
change.replace(mySplitResult[i], ticket.PNData[key]);
alert(change)
}
}
}
}
In JavaScript strings are immutable which means you must assign the result to a variable.
mySplitResult[i] = mychange.replace(mySplitResult[i], ticket.PNData[key]);