Does one push everytime creates array inside of array? [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 2 days ago.
Improve this question
I created button that is generate a random number every time , but in the console log it shows it like this:
small project i have been working on during course of javascript
I tried to make one array of random numbers and when it reach above 4 random numbers i will use Shift method to erase one. ( I also tried to use for loop )
What actually happen that is it kind of creates me new array inside of the orginal array
let randomArr = [];
if (randomArr.length >= 4) {
randomArr.shift();
lastNumbers.textContent -= randomArr + ",";
return randomArr;
} else {
randomArr.push(randomNumber);
// randomArr.length = randomArr;
lastNumbers.textContent += randomArr + ",";
// return randomArr;
}
Ty for helping!

Related

How does the following code work step by step? [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 1 year ago.
Improve this question
I came across this piece of code which checks if number of occurrences of an element in an array is greater than it is specified, and if yes it will remove the number:
function deleteNth(arr,x) {
var cache = {};
return arr.filter(function(n) {
cache[n] = (cache[n]||0) + 1;
return cache[n] <= x;
});
}
But I didn't understand the code from here: arr.filter(function(n){cache[n] = (cache[n]||0) + 1;return cache[n] <= x;});
Can anyone please explain in simple words what happens here and how does cache[n] part work.
Why is cache[n] incremented?
Thanks!
The arr.filter() begins by iterating over each item in the array and this case each item is represented by 'n'.
This item is then added to the empty object where 'n' is the key and the value is then incremented by one for each new item added to the object.
The return statement uses the cache to do a check of what 'n' values are less than or equal to x. If it returns false they are not added into the new array that is created. So if 'x' is 3 it will remove everything after the first three items from the array.
EDIT
Another way of writing the function which might make it more clear could be
function deleteNth(arr,x) {
return arr.filter((item, index) => {
if (index <= x) {
return item;
}
});
}

Integer comparison within score board app, score difference equal two [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'm writting an score board app.
Got two integers, i need to compare them, for example
to know when game should be ended, eg. score = 20:22, 24:26 (score difference to end the game should be equal to two)
How i can make such comparison with js?
You can use split() method to separate scores into two different elements, and then afterwards just compare them.
function isGameOver(score, differenceToWin) {
var scoreArray = score.split(":");
return Math.abs(scoreArray[0] - scoreArray[1]) > differenceToWin;
}
isGameOver('24:26', 2)
EDIT: In case you only need to compare two integers, go ahead and use only the return statement line:
var score1 = 24;
var score2 = 26;
var differenceToWin = 2;
var isGameOver = Math.abs(score1 - score2) > differenceToWin;

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/

Calculating array 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 6 years ago.
Improve this question
sorry i new learning about javascript:
i have code like this:
function call(arr) {
//i have no idea
}
console.log(call([1,2,3]));
i want to output:
//[6,3,2]
where result output obtained from [(2*3),(1*3),(1*2)]
It looks like you want each value in the array to be replaced with the value of all of the other elements multiplied together. You can calculate this more efficiently by first multiplying all of the numbers together, and then using division to take away just one number from the product at any one time, leaving the product of all the other numbers in the array.
Array.prototype.reduce
Array.prototype.map
function call(arr) {
var product = arr.reduce((a,b) => a * b, 1);
return arr.map(num => product / num);
}
console.log(call([1, 2, 3]));
Note that this won't work if a 0 appears anywhere in the array.

Regex in JavaScript for first SUM/MIN/MAX/AVG found in string [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
What I'm trying to do is the following:
SUM(Sales) + SUM(Revenue)
or MIN(Sales) + SUM(Revenue).
What I want is the first calculation. So for 1), the result "SUM" will be given, and for 2) the result "MIN" will be given.
I've tried this for if statements but it's either impossible, or incredibly difficult to do that way. Could anyone guide me on potentially a RegEx way of doing this?
What I tried in if statements:
function hasFormula(formulaToLower) {
// formulaToLower could equal "SUM(Sales) + SUM(Revenue)" etc
// could also equal "SUM(Sales) + MIN(Revenue)" - this will return MIN, but it return SUM.
if (formulaToLower.indexOf('sum') !== -1) {
return "SUM";
}
if (formulaToLower.indexOf('min') !== -1) {
return "MIN";
}
}
Obviously though, this will bring out MIN first, even if it's found second, and so on...
You can use a regexp that allows all the combinations you want. The matches will be returned in the correct order if you use the global modifier, or only the first one will be returned if you do not:
var matcher = /SUM|MIN|MAX|AVG/;
var str1 = 'SUM(Sales) + SUM(Revenue)';
var str2 = 'MIN(Sales) + SUM(Revenue)';
console.log(str1.match(matcher)[0]) // SUM
console.log(str2.match(matcher)[0]) // MIN
*The [0] part takes the first element in the array of results returned by match.

Categories

Resources