What I'm trying to achieve is to create a formatted number with thousands-delimiter from a simple string input.
So my input would look something like let input = "12345" and my expected return value should look like "12,345".
I know there are already several libraries, which take care of this, but I want to keep it simple and just do it by myself. My current solution is a bit redundant (because of the double .reverse()) and I'm pretty sure, that there is a better solution.
let array = input.split('');
array.reverse();
for (let i = 3; i < array.length; i += 4) {
array.splice(i, 0, ',');
}
array.reverse();
return array.join('');
I have made a similar answer for another question: Insert new element after each nt-h array elements. It is a generalized method that insert a token every N positions. The solution uses a while loop with the Array.splice() method. To meet your request, I have extended it to support start the inserts from the end of the array. Just another option...
let insertTokenEveryN = (arr, token, n, fromEnd) => {
// Clone the received array, so we don't mutate the
// original one. You can ignore this if you don't mind.
let a = arr.slice(0);
// Insert the <token> every <n> elements.
let idx = fromEnd ? a.length - n : n;
while ((fromEnd ? idx >= 1 : idx <= a.length))
{
a.splice(idx, 0, token);
idx = (fromEnd ? idx - n : idx + n + 1);
}
return a;
};
let array = Array.from("1234567890");
let res1 = insertTokenEveryN(array, ",", 3, true);
console.log(res1.join(""));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
But, obviously, like people commented, your best option for this will be using input.toLocaleString('en-US'):
let input = "1234567890";
console.log(Number(input).toLocaleString("en-US"));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
Although in your example the you finish with a string, the title says "into array". This is quite a compact way using lodash:
import { chunk, flatten } from 'lodash'
const ADD_EVERY = 5
const ITEM_TO_ADD = {}
const data = flatten(
chunk(input, ADD_EVERY).map((section) =>
section.length === ADD_EVERY ? [...section, ITEM_TO_ADD] : section
)
It is conceptually kind of similar to doing a split().join()
Here is a plain soluce but regex is better.
function separate(str, separator) {
// Handling the head case (from 0 to 2 size)
const headSize = str.length % 3;
let newFormat = headSize ? `${str.substr(0, headSize)}${separator}` : '';
// Handle every 3 character
const nbTripleChar = (str.length - headSize) / 3;
for (let i = 0; i < nbTripleChar; i += 1) {
newFormat = `${newFormat}${str.substr((i * 3) + headSize, 3)}`;
if ((i + 1) !== nbTripleChar) {
newFormat = `${newFormat}${separator}`;
}
}
return newFormat;
}
console.log(separate('12515', ','));
You can iterate backwards through the array and build the string using a second index.
The string concatenation might be costly but it only iterates the list once. You could also probably us .reduce() to do that without a for loop(since almost any array-iteration operation can be done as a function call these days);
let input = 123456789949949291;
let array = input.toString().split('');
let candidateString = '';
for (let i = array.length-1; i >=0; i--) {
candidateString=array[i]+candidateString;
let revIndex=array.length-i;
if(revIndex%3==0 && revIndex!== array.length){
candidateString = ','+candidateString;
}
}
console.log(candidateString);
You could replace by looking for a group of three characters to the end of the string.
var string = '12345678';
console.log(string.replace(/(?=(...)+$)/g, ','));
If you are not a fan of Regex. or builtins like toLocaleString() this should handle most cases you might encounter
function format(str) {
str = str.trim().replace(/\s+/g, '')
if (isNaN(str)) return 'NaN'
str = str.split('')
let strBuild = [str.pop()]
for (let number of strBuild) {
if (strBuild.length % 3 === 0) strBuild.unshift(str.pop() + ',')
else strBuild.unshift(str.pop())
if (!str.length) return strBuild.join('');
}
}
console.log(format('1 '))
console.log(format('1 a 2'))
console.log(format(' 12 '))
console.log(format('123 '))
console.log(format(' 123'))
console.log(format(' 1 2 3'))
console.log(format('1 2 3 '))
console.log(format('12 34'))
console.log(format('123 45678'))
console.log(format('12349 567 8'))
console.log(format(' 1234 9567 81 '))
console.log(format(' 1234 9567 81 9 7 5 6 4 5 8 '))
console.log(format(' 1234 9567 81 c '))
Related
I am trying to find an odd string from an given array.
Code :
const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];
function oddFriend(friendArray) {
for (let i = 0; i < friendArray.length; i++) {
if (friendArray[i].length % 2 != 0) {
return friendArray[i];
}
}
}
const myFriend = oddFriend(friendArray);
console.log(myFriend);
You can apply this:
const friendArray = ["agdum","bagdum","chagdum","lagdum","jagdum","magdum",];
function oddFriend(friendArray) {
if (friendArray.length % 2 != 0) {
return friendArray;
}
}
const myFriend = friendArray.filter(oddFriend);
console.log(myFriend);
.flatMap() and a ternary callback makes a simple and readable filter. It's not entirely clear if you wanted only odd or odd and even so there's both versions.
Odd length strings
const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];
let odd = fArray.flatMap(o => o.length % 2 === 1 ? [o] : []);
console.log(odd);
Odd & even length strings
const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];
let oe = [[], []];
fArray.forEach(o => o.length % 2 === 1 ? oe[0].push(o) : oe[1].push(o));
console.log(oe);
You can use Array.filter() method which will create a new array with all odd friends that pass the test implemented by the provided function.
// Input array
const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];
// Filtered result
console.log(friendArray.filter((friend) => friend.length % 2 !== 0));
Thanks to Nina I have a code to compare two sentences word by word and return the number of word matches like this:
function includeWords(wanted, seen) {
var wantedMap = wanted.split(/\s+/).reduce((m, s) => m.set(s, (m.get(s) || 0) + 1), new Map),
wantedArray = Array.from(wantedMap.keys()),
count = 0;
seen.split(/\s+/)
.forEach(s => {
var key = wantedArray.find(t => s === t || s.length > 3 && t.length > 3 && (s.startsWith(t) || t.startsWith(s)));
if (!wantedMap.get(key)) return;
console.log(s, key)
++count;
wantedMap.set(key, wantedMap.get(key) - 1);
});
return count;
}
let matches = includeWords('i was sent to earth to protect you introduced', 'they\'re were protecting him i knew that i was aware introducing');
console.log('Matched words: ' + matches);
The code works fine, but there is still one issue:
What if we want to return a match for introduced and introducing too?
If you want the program to consider the words 'introduce' and 'introducing' as a match, it would amount to a "fuzzy" match (non binary logic). One simple way of doing this would require more code, the algorithm of which would possibly resemble
Take 2 words that you wish to match, tokenize into ordered list
of letters
Compare positionally the respective letters, i.e
match a[0]==b[0]? a[1]==b[1] where a[0] represents the first letter
of the first word and b[0] represents the first tokenized
letter/character potential match candidate
KEep a rolling numeric count of such positional matches. In this case it is 8 (introduc).
divide by word length of a = 8/9 call this f
divide by word length of b = 8/11 call this g
Provide a threshold value beyond which the program will consider it a match. eg. if you say anything above 70% in BOTH f and g can be
considered a match - viola, you have your answer!
Please note that there is some normalization also needed to prevent low length words from becoming false positives. you can add a constraint that the aforementioned calculation applies to words with at least 5 letters(or something to that effect!
Hope this helps!!
Regards,
SR
You could calculate similarites for a word pair and get a relation how many characters are similar bei respecting the length of the given word and the wanted pattern.
function getSimilarity(a, b) {
var i = 0;
while (i < a.length) {
if (a[i] !== b[i]) break;
i++;
}
return i / Math.max(a.length, b.length);
}
console.log(getSimilarity('abcdefghij', 'abc')); // 0.3
console.log(getSimilarity('abcdefghij', 'abcdef')); // 0.6
console.log(getSimilarity('abcdefghij', 'abcdefghij')); // 1
console.log(getSimilarity('abcdef', 'abcdefghij')); // 0.6
console.log(getSimilarity('abcdefghij', 'abcdef')); // 0.6
console.log(getSimilarity('abcdefghij', 'xyz')); // 0
console.log(getSimilarity('introduced', 'introducing')); // 0.7272727272727273
Here's a quick fix solution.
It's not intended as a complete solution.
Since the English language has more than a few quirks that would almost require an AI to understand the language.
First add a function that can compare 2 words and returns a boolean.
It'll also make it easier to test for specific words, and adapt to what's really needed.
For example, here's a function that does the simple checks that were already used.
Plus an '...ed' versus '...ing' check.
function compareWords (word1, word2) {
if (word1 === word2)
return true;
if (word1.length > 3 && word2.length > 3) {
if (word1.startsWith(word2) || word2.startsWith(word1))
return true;
if (word1.length > 4 && word2.length > 4) {
if (/(ing|ed)$/.test(word1) && word1.replace(/(ing|ed)$/, 'inged') === word2.replace(/(ing|ed)$/, 'inged'))
return true;
}
}
return false;
}
//
// tests
//
let words = [
["same", "same"],
["different", "unsame"],
["priced", "pricing"],
["price", "priced"],
["producing", "produced"],
["produced", "producing"]
];
words.forEach( (arr, idx) => {
let word1= arr[0];
let word2= arr[1];
let isSame = compareWords(word1, word2);
console.log(`[${word1}] ≈ [${word2}] : ${isSame}`);
});
Then use it in the code you already have.
...
seen.split(/\s+/)
.forEach(s => {
var key = wantedArray.find(t => compareWords(t, s));
...
Regarding string similarity, here's f.e. an older SO post that has some methods to compare strings : Compare Strings Javascript Return %of Likely
I have implemented this, it seems to work fine. any suggestions would be appreciated..
let speechResult = "i was sent to earth to introducing protect yourself introduced seen";
let expectSt = ['they were protecting him knew introducing that you i seen was aware seen introducing'];
// Create arrays of words from above sentences
let speechResultWords = speechResult.split(/\s+/);
let expectStWords = expectSt[0].split(/\s+/);
function includeWords(){
// Declare a variable to hold the count number of matches
let arr = [];
for(let a = 0; a < speechResultWords.length; a++){
for(let b = 0; b < expectStWords.length; b++){
if(similarity(speechResultWords[a], expectStWords[b]) > 69){
arr.push(speechResultWords[a]);
console.log(speechResultWords[a] + ' includes in ' + expectStWords[b]);
}
} // End of first for loop
} // End of second for loop
let uniq = [...new Set(arr)];
return uniq.length;
};
let result = includeWords();
console.log(result)
// The algorithmn
function similarity(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength)*100;
}
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
Question 2: The input consist of a string, "o" represents odd number, "e" represents even number to be printed
Example 1.
Input: 8-e
Expected Output: 2|4|6|8
Example 2.
Input: 6-o
Expected Output: 1|3|5
Example 3.
Input: 1-o
Expected Output: 1
if have tried with for loop, but I'am a beginner so I'am confused with(-e)
const evenOdd = (number) => {
let evenvalue = [];
let oddValue=[];
for(let i =0; i<=number; i++){
if(number%i==0)
evenvalue.push(i);
console.log(evenvalue);
}if(number%i!=0){
oddValue.push(i);
console.log(oddValue);
}
};
evenOdd(9);
You could take a while statement and get a start value of one plus an offset of one if the wanted type is even. Then iterate and add the value to the result set until the value is greater than the maximum value.
function fn(request) {
var [max, type] = request.split('-'),
i = 1 + (type === 'e'),
result = [];
while (i <= max) {
result.push(i);
i += 2;
}
return result;
}
console.log(...fn('8-e'));
console.log(...fn('6-o'));
console.log(...fn('1-o'));
You will need to extract the letter and the number from you string first. One easy way to do that :
const evenOdd = (s) => {
let odd = s.length-1 ==='o';
let number = Number(s.substring(0, s.length-2));
let evenvalue = [];
...
if(odd){...} else {...}
};
You could also use split() or if the pattern was more complicated, a Regex.
You can split on - and add based on type add values upto the number
Split the given input by -, first value represents max number and second represents it's type
Check the type if it is even add the even values start from 2 and upto to the max number else start from 1, and join them with | in the end
let func = (input) => {
let [num, type] = input.split('-')
let arr = []
let i = 1 + (type === 'e')
while (i <= num) {
arr.push(i)
i += 2
}
return arr.join('|')
}
console.log(func('8-e'))
console.log(func('1-o'))
console.log(func('6-o'))
Basically, don't supply a number to the function, supply a string and then parse the string. That is, don't try and give the function 9-e, give it '9-e'.
Get the parts of the input by splitting on -.
Turn the number into a number.
Give 0 for even, 1 for odd (x % 2 is 0 for even number, 1 for odd).
Build the results.
function listNumbers(constraint)
{
const parts = constraint.split('-');
const number = Number(parts[0]);
const numberType = parts[1] === 'e' ? 0:1;
let result = [];
for(let i = 1; i <= number; i++)
{
if(i%2 === numberType)
{
result.push(i);
}
}
return result;
}
console.log(listNumbers('8-e'));
Or if you want make the code look clever:
function listNumbers(constraint)
{
const parts = constraint.split('-');
const number = Number(parts[0]);
const numberType = parts[1] === 'e' ? 0:1;
return Array.from(Array(number), (x,i) => i + 1 ).filter(x => x%2 == numberType);
}
console.log(listNumbers('8-e'));
Lilah has a string, s, of lowercase English letters that she repeated infinitely many times.
Given an integer, n, find and print the number of letter 'a' in the first n letters of Lilah's infinite string.
This is my solution, but it is not correct, and I'm struggling to figure out why:
function repeatedString(s, n) {
let counter = 0;
const remainder = n % s.length;
const substring = s.substring(0, remainder);
const concatString = s + substring;
for (let letter of concatString) {
if (letter === 'a') {
counter++;
}
}
return (counter * n);
}
const str = "dhfgjhdfoiahwiuerhiguhzlkjvxzlkjghatriaeriaauih";
console.log(
repeatedString(str, 20)
);
I think it may be the
const concatString = s + substring;
would you please just reference the substring instead...
for (let letter of substring) {
if (letter === 'a') {
counter++;
}
}
return counter
You just need to loop through the s.substring(0, n) and return the counter value, (counter * n) doesn't make any sense.
function repeatedString(s, n) {
let counter = 0;
//const remainder = n % s.length;
const substring = s.substring(0, n);
console.log(substring);
//const concatString = s + substring;
for (let letter of substring) {
if (letter === 'a') {
counter++;
}
}
return counter;
}
const str = "dhfgjhdfoiahwiuerhiguhzlkjvxzlkjghatriaeriaauih";
console.log(
repeatedString(str, 20)
);
You can do that in following steps:
Get the count of the letter in given string. i.e c1.
Then get the part of the substring using slice(). The part will start of 0 up the the remainder of n and length of string.(c2)
Then multiply c1 with the number of times the given string will be in string of length n. i.e c1 * Math.floor(n/str.length)
Add the other count of remaining part c2 to the result and return
You can do that using filter() and check the count of given letter in given string. And then multiply it with no of times the string will repeat in for length n.
function func(str,l,n){
let c1 = [...str].filter(x => x === l).length;
let c2 = [...str.slice(0,n%str.length)].filter(x => x === l).length;
return (c1 * Math.floor(n/str.length)) + c2;
}
console.log(func('abcac','a',10))
This should give you the number of times a appears in the numbers of n length
const input = s;
var subStr = input.substr(0,n).split('');
console.log(subStr);
var output = 0;
subStr.forEach((e) => {
if (e === 'a') {
console.log(e);
output++;
}
})
console.log(output);
Using the next link as the source of the question:
https://medium.com/#yashka.troy/want-to-know-the-major-player-in-programming-18f2d35d91f7
Where it is explained better:
Lilah has a string, s, of lowercase English letters that she repeated infinitely many times.
Given an integer, n, find and print the number of letter a's in the first letters of Lilah's infinite string.
For example, if the string s=’abcac’ and n=10, the sub string we consider is ‘abcacabcac’, the first 10 characters of her infinite string. There are 4 occurrences of a in the substring.
A solution could be:
function repeatedString(s, n)
{
let res = 0;
const remainder = s.slice(0, n % s.length);
// Get number of times 'a' is present on "n / s.length" repetitions.
for (let letter of s)
{
if (letter === 'a') res++;
}
res *= Math.floor(n / s.length);
// Add the number of times 'a' is present on the remainder.
for (let letter of remainder)
{
if (letter === 'a') res++;
}
return res;
}
const str = "abca";
console.log(`${repeatedString(str, 10)} a's on first 10 letters:`);
console.log(`${repeatedString(str, 4)} a's on first 4 letters:`);
console.log(`${repeatedString(str, 0)} a's on first 0 letters:`);
console.log(`${repeatedString(str, 22)} a's on first 22 letters:`);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
I was given the challenge of converting a string of digits into 'fake binary' on Codewars.com, and I am to convert each individual digit into a 0 or a 1, if the number is less than 5 it should become a 0, and if it's 5 or over it should become a 1. I know how to analyze the whole string's value like so:
function fakeBin(x){
if (x < 5)
return 0;
else return 1;
}
This however, analyzes the value of the whole string, how would I go about analyzing each individual digit within the string rather than the whole thing?
Note: I have already looked at the solutions on the website and don't understand them, I'm not cheating.
You can do it in one line with two simple global string replacement operations:
function fakeBin(x){
return ("" + x).replace(/[0-4]/g,'0').replace(/[5-9]/g,'1');
}
console.log(fakeBin(1259))
console.log(fakeBin(7815))
console.log(fakeBin("1234567890"))
The ("" + x) part is just to ensure you have a string to work with, so the function can take numbers or strings as input (as in my example calls above).
Simple javascript solution to achieve expected solution
function fakeBin(x){
x = x+'' ;
var z =[];
for(var i=0;i< x.length;i++){
if((x[i]*1)<5){
z[i] =0;
}else{
z[i]=1;
}
}
return z
}
console.log(fakeBin(357))
The snippet below will take a string and return a new string comprised of zeros and/or ones based on what you described.
We use a for ...of loop to traverse the input string and will add a 0 or 1 to our return array based on whether the parsed int if greater or less than 5.
Also note that we are checking and throwing an error if the character is not a number.
const word = "1639";
const stringToBinary = function(str) {
let ret = [];
for (const char of word) {
if (Number.isNaN(parseInt(char, 10))) {
throw new Error(`${char} is not a digit!`);
} else {
const intVal = parseInt(char, 10);
ret.push(intVal > 5 ? 1 : 0);
}
}
return ret.join('');
};
console.log(stringToBinary(word));
if you are in java you can use
charAt()
and you make a for with the word length and you can check one by one
for(int i = 0; i < text.length(); i++){
yourfunction(texto.charAt(i));
}
Split the string and apply the current function you have to each element of the string. You can accomplish this with map or with reduce:
function fakeBin(x) {
x = x.split('');
let toBin = x => {
if (x < 5)
return 0;
else return 1
}
return x.map(toBin).join('');
}
console.log(fakeBin("2351"));
refactored
function fakeBin(x) {
x = [...x];
let toBin = x => x < 5 ? 0 : 1;
return x.map(toBin).join('');
}
console.log(fakeBin("2351"));
reduce
function fakeBin(x) {
let toBin = x => x < 5 ? 0 : 1;
return [...x].reduce((acc,val) => acc + toBin(val), "");
}
console.log(fakeBin("23519"));
You can use String.prototype.replace() with RegExp /([0-4])|([5-9])/g to match 0-4, 5-9, replace with 0, 1 respectively
let str = "8539734222673566";
let res = str.replace(/([0-4])|([5-9])/g, (_, p1, p2) => p1 ? 0 : 1);
console.log(res);