Javascript - Show all telephone numbers in a range [closed] - javascript

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 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.

Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.

Related

React -- Why does this line append rather than add [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 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);

Javascript returns and calling another function question [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 16 days ago.
This post was edited and submitted for review 16 days ago.
Improve this question
I need to write code to
Use a prompt() to gather an initial dollar amount from the user and then pass that value into a calculation function(already created) called calculation(). Then I need to take the return of that function and pass it to the current function. The returned, properly formatted value (the calculation function formats numbers), should appear in an alert() box.
Function format(){
cashAmount = Number(prompt("Enter amount to be formatted"));
}
This is as far as I've gotten. I'm confused at the function returning another function.
Try using these lines.
If the boolean parameter separatedByComma is true we ara going to use the Number method toLocalString("en-US") return the number separated by commas. If the parameter is false it's return a foramted number without commas "$533535".
const ashAmount = Number(prompt("Enter amount to be formatted"));
function format(ashAmount, separatedByComma) {
return separatedByComma
? `$${ashAmount.toLocaleString("en-US")}`
: `$${ashAmount}`;
}
format(ashAmount, true);

How to extract number from backwards in javascript in optimised way [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 3 years ago.
Improve this question
I have to extract a string from backwards upto 10 digits.
Use case is when we select a mobile number it is prefixed with country code sometimes and sometimes we get only the mobile number.
Let's say the number is : +91-84040355236545
I have to extract the number from the end say from 5 to last 10 degits so the end result would be 0355236545
I have a solution of using string.substring method
Here's a simple solution using substrings:
var number = "+91-84040355236545";
var lastTenDigitsNumber = number.substr(number.length - 10);
console.log(lastTenDigitsNumber);
A simpler solution is using slice:
var number = "+91-84040355236545";
console.log(number.slice(-10));
Another solution using a function and RegEX:
function extractTenDigits(number) {
var rx = /(\d{10}$)/g;
var arr = rx.exec(number);
return arr[1];
}
var number = "+91-84040355236545";
console.log(extractTenDigits(number));
I did a simple benchmark and the best solution for small quantity of numbers is the one using slice.
If you provide more details I can provide a more tailored suggestion.
You can try the following using substring :
let str = "+91-84040355236545";
str.substr(str.length - 10 ,10);

How to calculate all possible results in a 4x3 array? [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 4 years ago.
Improve this question
assume that the array looks like this.
var res = [2,4,1,6,
1,1,2,8,
5,6,7,1];
all possible four quadrants combinations of this array are 81.
in this example we have only one combination on index : 4,5,2,11 which is four 1.
my question is how to calculate them.
thanks.
I'm not completely clear on what you're trying to achieve. Do you want to find every subset of length exactly 4 which contains all the same value? If so, you can do this in N^2 time with the following naive algorithm:
let quadrants = [];
res.forEach(checkElement => {
let possibility = [];
res.forEach((element, index) => {
if (element === checkElement) {
possibility.push(index);
}
});
if (possibility.length === 4) {
quadrants.push(possibility);
}
});
If you want to account for the possibility of the original array having more than 4 of the same number and include all subset quadrants, you'll need to change the length check to >=4 and add one more step at the end of this: calculate the power set of all listed quadrants with length greater than 4, filter out the ones that aren't length 4, and then concatenate those to the quadrants array. (You'll want to remove each quadrant with length >4 from the quadrants array before calculating its power set, so it won't be in the final result.)
If you do that, you may be able to optimize the last step by only calculating the subsets of length 4 of the longer quadrants; try using this as a guide for that if you need it: https://www.geeksforgeeks.org/print-subsets-given-size-set/

Using a while loop to print random numbers until a certain number is reached [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 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.

Categories