I written previously to calculate for client using javascript
This is my function
var check = {}, string = inputString, count = 0;
var occurrence;
$.each(string.split(''), function(){
if(!check[this]){
count++;
check[this]=true;
})
The function above I use it to calculate number occurrence in a string
For example
number 1234 will return 4, as there are number 1,2,3 and 4
number 1233 will return 3, as the unique number are 1,2 and 3
number 1212 will return 2, as the unique number are 1 and 2
I wonder how do I convert my function above into a working php function that return the same count.
The next thing is I written another check which is getMostOccurrence
The purpose is for example
number 1212, will return 2, as 1 appear 2 times, and 2 appear 2 times
However if number
1112, the result will be 3, as 1 appear 3 times
How do I do this count function in php
Thanks for helping!!
Use count_chars
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
Example taken straight from PHP.net
<?php
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
?>
You can use it to fulfil your second purpose very easily as well.
Related
I'm attempting to do some validation a price field. I would like to check if the price entered into the price field ends in .99
I've attempted find posts about this but I can't find examples for decimal numbers only whole numbers. I tried to check by doing price % 1 but it isnt consistent as the price increases by 10, 20 etc.
Is there a quick way to check if all numbers end in .99?
const price = 9.99
console.log(price % 1)
Floating point math is inherently imprecise. The actual mathematical expression price - 9 will get those extra 0s and a 2 too.
Best you could do is convert to a string with fixed precision (rounding off any extraneous precision; for a price in dollars, you'd only need two digits, but you might go to three or more to verify the price entered didn't end with nonsense fractions of a cent) and perform a string test, e.g.
price.toFixed(2).endsWith('.99')
which doesn't try to perform math on price at all, it just rounds off to two digits after the decimal place to produce a string, then checks if the string ends with .99.
You can try regular expression as well. See following code for example:
function testRegex() {
var re = /^[0-9]*[.](99)$/;
var val = document.getElementById("inputValue").value;
if(re.exec(val)) {
document.getElementById("result").innerText = "Found match!!!";
} else {
document.getElementById("result").innerText = "Found no match!!!";
}
}
<input type="text" id="inputValue" value="" onkeyup="testRegex()" />
<div id="result"></div>
You can perform that validation using the following regular expression:
/\.99$/
First, we try to match an explicit . by escaping it with a backlash. Then, the next two characters must be 99, and then the string end must occur for the successful match. We check that with $. For example,
prices = [0.99, 0.0099, 1.99, 2.99, 3.98, 4.01];
for (const price of prices) {
if (/\.99$/.test(price.toString())) {
console.log(`${price} ends with .99`);
}
}
will print:
0.99 ends with .99
1.99 ends with .99
2.99 ends with .99
Here's an example of the customer codes:
C000000123
C000000456
If I input C123 in the search box, "C000000123" will automatically display.
9 numbers are fixed.
Please help me, a short sample was shown to me but I don't get it.
function test(key, num, digit) {
let retStr;
xxxx (condition)
retun retStr;
}
here's an elaboration:
**
input:123
output:A00000123
input:1
output:A00000001
input:99999
output:A00099999
**
here's the detailed demand:
Since it takes time and effort to enter the management number “alphabet + numeric value 9 digits” on the search screen, when the alphabetic number and the number excluding the leading 0 are entered, it is automatically complemented so that it becomes 9 padded with zeros.
sorry i'm very very new to programming in javascript
Try this:
May be what you want...
Please test it and tell if its what you want.
function getOutput(input){
var str=input.substring(1,input.length);
var padd0=9-str.length;
var zr="000000000";
var zrsub=zr.substring(0,padd0);
var output=input[0]+zrsub+""+str;
return output;
}
//Example: Call it like (NB any letter can be used):
getOutput("C123"); //or
getOutput("D123");
You can use .endsWith in js which takes a string and a search string and returns true if the specified string ends with the search string.
This function takes an array of customer ids and a search string and returns the matching customer id
function searchCustomer(customers, searchString) {
return customers.find(customer => customer.endsWith(searchString));
}
searchCustomer(['C000000123', 'C000000456'], 123); // "C000000123"
searchCustomer(['C000000123', 'C000000456'], 456); // "C000000456"
searchCustomer(['C000000123', 'C000000456', 'A00000001'], 1); //"A00000001"
I did a bit of coding in javascript and I don't understand the problem...
My goal is to get every divisor of a given number and check if the sum of them is greater than the number itself.
The divisors should include one, but not the number itself.
I made 2 functions to separate the code and make it more readable for now.
In the first 12 number, the condition apply only for the number 12 because 1+2+3+4+6=16 which is greater than 12 and it shows it correctly, but when I try the function with the first 20 number, only 18 and 20 are shown, when 12 is clearly good. It disappears when the loop reaches the number 16.
Here is my code:
function getDivisors(n){
var divisors=new Array();
for(var x=1;x<n;x++){
if(n%x==0) divisors.push(x);
}
return divisors;
}
function getNumbers(n){
var numbers=new Array(),
sum=0;
for(var x=1;x<=n;x++){
sum=getDivisors(x).reduce((a, b) => a + b, 0);
if(sum>n) numbers.push(x);
console.log("Number: "+x+" sum:"+sum);
}
return numbers;
}
console.log(getNumbers(20));
You should have if (sum>x) instead of if (sum>n) inside the for loop in getNumbers(n).
I would like to create a program that takes a number is input, such as: 12345 and then splits this number into 2 digit numbers and store it in a array. The array must look like this: [0]=45 [1]=23 [2]=1 . This means that the splitting of the numbers must start from the last digit of the number and not the first.
This is what I have until now:
var splitCount = []; // This is the array in which we store our split numbers
//Getting api results via jQuery's GET request
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) {
//result is our api answer and contains the recieved data
//now we put the subscriber count into another variable (count); this is just for clarity
count = result.items[0].statistics.subscriberCount;
//While the subscriber count still has characters
while (count.length) {
splitCount.push(count.substr(0, 2)); //Push first two characters into the splitCount array from line 1
count = count.substr(2); //Remove first two characters from the count string
}
console.log(splitCount) //Output our splitCount array
});
but the problem with this is that if there are 5 digits for example: 12345 the the last digit will be in an array by itself like this: [0]=12 [1]=34 [2]=5 but I need the last array to have 2 digits and the first should be the one with one digit instead like this: [0]=1 [1]=23 [2]=45
very crude but this should work assuming the string is always numbers:
input = "12345"
def chop_it_up(input)
o = []
while input.length > 0
if input.length <= 2
o << input
else
o << input[-2..input.length]
end
input = input[0..-3]
chop_it_up(input)
end
return o
end
I probably do sth like this :
int[] fun(int x){
int xtmp = x;
int i = 0;
int len = String.valueOf(x).length();
// this is a function for java, but you can probably find
//an equivalent in whatever language you use
int tab[(len+1)/2];
while(xtmp > 1){
tab[i] = xtmp%100;
xtmp = int(xtmp/100); // here you take the integer part of your xtmp
++i;
}
return tab;
}
This question already has answers here:
Ordinals in words javascript
(3 answers)
Closed 9 years ago.
Is there any inbuilt js/jquery function that converts 1 to first, 2 to second, 3 to third... etc.?
ex:
Num2Str(1); //returns first;
Num2str(2); //returns second;
I dont want to write a function for 100 numbers. Please help.
There is no inbuilt function for it.
I did write one for up to 99:
var special = ['zeroth','first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'];
var deca = ['twent', 'thirt', 'fort', 'fift', 'sixt', 'sevent', 'eight', 'ninet'];
function stringifyNumber(n) {
if (n < 20) return special[n];
if (n%10 === 0) return deca[Math.floor(n/10)-2] + 'ieth';
return deca[Math.floor(n/10)-2] + 'y-' + special[n%10];
}
// TEST LOOP SHOWING RESULTS
for (var i=0; i<100; i++) console.log(stringifyNumber(i));
DEMO: http://jsbin.com/AqetiNOt/1/edit
You could create a numberbuilder:
You will need to create a foolproof way to convert the single digits by power to a string.
1234 -->1(one)*10^3(thousand)+2(two)*10^2(hundred)+3(three)10(ten)+4(four)(one)
==> one thousand two hundred th irty four th
123456 --> one hundred tw enty three thousand four hundred fi fty six th
if you are wondering about the notation: I tried to split this up in the single decision steps you need to make
the rules for building repeat every three digits. The rest is up to you.
Oh and before I forget: there is only "3" exceptions to the th-rule. one, two and three.