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 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])
Related
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 15 days ago.
Improve this question
I have other ways of doing the same thing .. So I am not looking for a different solution to this problem ... I am looking for an explanation as to why if I have defined a integer, it still concatenates with .map as if it were a string.
I have a basic set of data retrieved from an API:
"data":["8","8","12","1","7","4","2"]
If I map it using
let count = response.data.metrics.data.map((item) => + parseInt(item));
I am having a hard time understanding why it's treating this as a string returning
88121743
When I feel like because I am parsing it as an integer it should add and come out with 42.
Is this just an issue just using .map? Can shortcut math functions be used here?
Here is my Reproducible Example
Your current approach using Array#map creates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use Array#reduce with parseFloat/parseInt, the Number function, or the unary plus operator to convert each string to a number.
const visitCount = data.reduce((a,b) => a + parseFloat(b), 0);
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 19 days ago.
Improve this question
Below is the screen of my VScode, I am declaring arr1 as an array of numbers and later reversing it.Input
The problem is that on output the 1st paragraph is showing the reversed arrays instead of original one. What is the exact problem?
Output
Try this:
let reverse = Array.from(arr1).reverse()
As reverse() method modifies the original array (arr1), then you need to create a new array with Array.from(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
Reverse is a mutating method, so both arr1 and reverse reference the same array. You'll want to do something like const reverse = [...arr1].reverse() instead to ensure that you aren't mutating the original array.
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 years ago.
Improve this question
I'm trying to replace string
'null-null-1234' to '***-**-1234'
const p = 'null-null-1234';
const regex = /null/gi;
console.log(p.replace(regex, '***'));
Output => "***-***-1234" NOT AS EXPECTED
p.replace(/null-null/, '***-**')
I think what you actually want to do is to make a distinction between first and second values.
Hence, you probably need 2 regexes.
p.replace(/^null/, '***').replace(/-null-/, '-**-')
The first one will replace the null in first position (if any) by 3 stars, and the second replace will replace a null in the middle (if any) by 2 stars.
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 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.
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 7 years ago.
Improve this question
So I have a function "profile" that returns an array.
And I have a function "TMYW" whose results I want to add to the array that profile returns. So I tried something like this:
profile(m.hours).push(TMYW(m.hours));
Interestingly this whole statement returns the integer 3.
Just curious as to why this happens. Obviously it is easy to get the desired result by doing something like this:
var b = profile(m.hours);
b.push(TMYW(m.hours));
See the following regarding the use of Array.prototype.push():
The push() method adds one or more elements to the end of an array and returns the new length of the array.
So in your case:
var b = profile(m.hours); // returns an array
b.push(TMYW(m.hours)); // returns the new length of b