I am trying to solve the problem Basic Algorithm Scripting: Truncate a String; but is not working properly...
let truncateString = (str, num) => {
return (str.length <= num ? str : str.slice(0, num > 3 ? num - 3 : num) + '...');
};
truncateString("A-tisket a-tasket A green and yellow basket", 8);
// running tests
truncateString("A-tisket a-tasket A green and yellow basket", 8) should return "A-tisket...".
truncateString("Peter Piper picked a peck of pickled peppers", 11) should return "Peter Piper...".
// tests completed
It's even simpler than your code - change your ternary operator a little:
let truncateString = (str, num) => str.length > num ? str.slice(0, num) + "..." : str;
console.log(truncateString("A-tisket a-tasket A green and yellow basket", 8));
console.log(truncateString("Peter Piper picked a peck of pickled peppers", 11));
Here: (works)
let truncateString = (str, num) => {
return (str.length <= num ? str : str.slice(0, num) + '...');
};
let truncateString = (str, num) => {
const x = str.length,
y = num,
min = y ^ ((x ^ y) & -(x < y));
return str.slice(0, min) + (!(y-min) && '...');
};
Related
I have a signed value given as a hex number, by example 0xffeb and want convert it into -21 as a "normal" Javascript integer.
I have written some code so far:
function toBinary(a) { //: String
var r = '';
var binCounter = 0;
while (a > 0) {
r = a%2 + r;
a = Math.floor(a/2);
}
return r;
}
function twoscompl(a) { //: int
var l = toBinaryFill(a).length;
var msb = a >>> (l-1);
if (msb == 0) {
return a;
}
a = a-1;
var str = toBinary(a);
var nstr = '';
for (var i = 0; i < str.length; i++) {
nstr += str.charAt(i) == '1' ? '0' : '1';
}
return (-1)*parseInt(nstr);
}
The problem is, that my function returns 1 as MSB for both numbers because only at the MSB of the binary representation "string" is looked. And for this case both numbers are 1:
-21 => 0xffeb => 1111 1111 1110 1011
21 => 0x15 => 1 0101
Have you any idea to implement this more efficient and nicer?
Greetings,
mythbu
Use parseInt() to convert (which just accepts your hex string):
parseInt(a);
Then use a mask to figure out if the MSB is set:
a & 0x8000
If that returns a nonzero value, you know it is negative.
To wrap it all up:
a = "0xffeb";
a = parseInt(a, 16);
if ((a & 0x8000) > 0) {
a = a - 0x10000;
}
Note that this only works for 16-bit integers (short in C). If you have a 32-bit integer, you'll need a different mask and subtraction.
I came up with this
function hexToInt(hex) {
if (hex.length % 2 != 0) {
hex = "0" + hex;
}
var num = parseInt(hex, 16);
var maxVal = Math.pow(2, hex.length / 2 * 8);
if (num > maxVal / 2 - 1) {
num = num - maxVal
}
return num;
}
And usage:
var res = hexToInt("FF"); // -1
res = hexToInt("A"); // same as "0A", 10
res = hexToInt("FFF"); // same as "0FFF", 4095
res = hexToInt("FFFF"); // -1
So basically the hex conversion range depends on hex's length, ant this is what I was looking for. Hope it helps.
Based on #Bart Friederichs I've come with:
function HexToSignedInt(num, numSize) {
var val = {
mask: 0x8 * Math.pow(16, numSize-1), // 0x8000 if numSize = 4
sub: -0x1 * Math.pow(16, numSize) //-0x10000 if numSize = 4
}
if((parseInt(num, 16) & val.mask) > 0) { //negative
return (val.sub + parseInt(num, 16))
}else { //positive
return (parseInt(num,16))
}
}
so now you can specify the exact length (in nibbles).
var numberToConvert = "CB8";
HexToSignedInt(numberToConvert, 3);
//expected output: -840
function hexToSignedInt(hex) {
if (hex.length % 2 != 0) {
hex = "0" + hex;
}
var num = parseInt(hex, 16);
var maxVal = Math.pow(2, hex.length / 2 * 8);
if (num > maxVal / 2 - 1) {
num = num - maxVal
}
return num;
}
function hexToUnsignedInt(hex){
return parseInt(hex,16);
}
the first for signed integer and
the second for unsigned integer
As I had to turn absolute numeric values to int32 values that range from -2^24 to 2^24-1,
I came up with this solution, you just have to change your input into a number through parseInt(hex, 16), in your case, nBytes is 2.
function toSignedInt(value, nBytes) { // 0 <= value < 2^nbytes*4, nBytes >= 1,
var hexMask = '0x80' + '00'.repeat(nBytes - 1);
var intMask = parseInt(hexMask, 16);
if (value >= intMask) {
value = value - intMask * 2;
}
return value;
}
var vals = [ // expected output
'0x00', // 0
'0xFF', // 255
'0xFFFFFF', // 2^24 - 1 = 16777215
'0x7FFFFFFF', // 2^31 -1 = 2147483647
'0x80000000', // -2^31 = -2147483648
'0x80000001', // -2^31 + 1 = -2147483647
'0xFFFFFFFF', // -1
];
for (var hex of vals) {
var num = parseInt(hex, 16);
var result = toSignedInt(num, 4);
console.log(hex, num, result);
}
var sampleInput = '0xffeb';
var sampleResult = toSignedInt(parseInt(sampleInput, 16), 2);
console.log(sampleInput, sampleResult); // "0xffeb", -21
Based on the accepted answer, expand to longer number types:
function parseSignedShort(str) {
const i = parseInt(str, 16);
return i >= 0x8000 ? i - 0x10000 : i;
}
parseSignedShort("0xffeb"); // -21
function parseSignedInt(str) {
const i = parseInt(str, 16);
return i >= 0x80000000 ? i - 0x100000000 : i;
}
parseSignedInt("0xffffffeb"); // -21
// Depends on new JS feature. Only supported after ES2020
function parseSignedLong(str) {
if (!str.toLowerCase().startsWith("0x"))
str = "0x" + str;
const i = BigInt(str);
return Number(i >= 0x8000000000000000n ? i - 0x10000000000000000n : i);
}
parseSignedLong("0xffffffffffffffeb"); // -21
I want to encrypt words cycle shift value to each letter from the end of its word java script.
The value of this cycle shift is determined by the postion of the letter from the end of it's word. The shift value for each letter of word is its index value (starting from 0) right most character of the word.
function encryptWords(str) {
// you can comment this line
str = str.toLowerCase();
var result = '';
var charcode = 0;
for (var i = str.length -1 ; i > = 0; i--) {
charcode = (str[i].charCodeAt()) + i;
result += String.fromCharCode(charcode);
}
return result;
}
console.log(encryptWords('yum feed'));
for example, the shift values in "yum feed" are
yum : m-> 0, u -> 1, y -> 2;
feed : d->0, e->1, e->2, f->3
"avm igfd"
Your code works fine for a single word.
But for a string containing 2 or more words, you can try using Array.map() twice: once for words and once for chars in those words.
function CaesarCipher(str, num) {
// you can comment this line
str = str.toLowerCase();
var charcode = 0;
var result = str.split(' ').map(
val => val.split('').map(
(c, i) => {
// shifting of the chars, based on their index values
charcode = 97 + (
(c.charCodeAt() + val.length - 1 - i) - 97
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shift
) % 26;
// ^^^^ to avoid chars higher than 'z'
return String.fromCharCode(charcode);
}
).join('')
).join(' ');
return result;
}
console.log(CaesarCipher('yum feed', 2));
function encryptWords(str) {
return str
.split(' ')
.map(s => [...s]
.map((c, i) => String.fromCharCode(97 + (c.charCodeAt(0) + s.length - i - 98) % 26))
.join('')
)
.join(' ');
}
console.log(encryptWords('yum feed'));
Let's say I have an integer I and want to get the count of 1s in its binary form.
I am currently using the following code.
Number(i.toString(2).split("").sort().join("")).toString().length;
Is there a faster way to do this? I am thinking about using bitwise operators. Any thoughts?
NOTE: i is within the 32-bit limitation.
You can use a strategy from this collection of Bit Twiddling Hacks:
function bitCount (n) {
n = n - ((n >> 1) & 0x55555555)
n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24
}
console.log(bitCount(0xFF)) //=> 8
Note that the above strategy only works for 32-bit integers (a limitation of bitwise operators in JavaScript).
A more general approach for larger integers would involve counting 32-bit chunks individually (thanks to harold for the inspiration):
function bitCount (n) {
var bits = 0
while (n !== 0) {
bits += bitCount32(n | 0)
n /= 0x100000000
}
return bits
}
function bitCount32 (n) {
n = n - ((n >> 1) & 0x55555555)
n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24
}
console.log(bitCount(Math.pow(2, 53) - 1)) //=> 53
You could also use a regular expression:
function bitCount (n) {
return n.toString(2).match(/1/g).length
}
console.log(bitCount(0xFF)) //=> 8
A recursive very nice but slow way:
function count1(n, accumulator=0) {
if (n === 0) {
return accumulator
}
return count1(n/2, accumulator+(n&1))
}
console.log(count1(Number.MAX_SAFE_INTEGER));
But if you want a very fast one (faster than T.J. Crowder answer)):
count1s=(n)=>n.toString(2).replace(/0/g,"").length
console.log(count1s(Number.MAX_SAFE_INTEGER));
Note: some of the other solutions do not work with bit integers (> 32 bit)
these two do!
Now, if we consider only 32 bit numbers, the fastest way is this:
function count1s32(i) {
var count = 0;
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0f0f0f0f;
i = i + (i >> 8);
i = i + (i >> 16);
count += i & 0x3f;
return count;
}
console.log(count1s32(0xffffffff));
https://jsperf.com/count-1/1
53 bit comparison:
32 bit comparison:
Benchmark here! (since jsperf is often down).
function log(data) {
document.getElementById("log").textContent += data + "\n";
}
benchmark = (() => {
time_function = function(ms, f, num) {
var z;
var t = new Date().getTime();
for (z = 0;
((new Date().getTime() - t) < ms); z++) f(num);
return (z / ms)
} // returns how many times the function was run in "ms" milliseconds.
// two sequential loops
count1s = (n) => n.toString(2).replace(/0/g, "").length
// three loops and a function.
count1j = (n) => n.toString(2).split('').filter(v => +v).length
/* Excluded from test because it's too slow :D
function count1(n, accumulator=0) {
if (n === 0) {
return accumulator
}
return count1(n / 2, accumulator + (n & 1))
}
*/
function countOnes(i) {
var str = i.toString(2);
var n;
var count = 0;
for (n = 0; n < str.length; ++n) {
if (str[n] === "1") {
++count;
}
}
return count;
} // two sequential loops ( one is the toString(2) )
function count1sb(num) {
i = Math.floor(num / 0x100000000);
// if (i > 0) {
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0f0f0f0f;
i = i + (i >> 8);
i = i + (i >> 16);
count = i & 0x3f;
i = num & 0xffffffff;
// }
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0f0f0f0f;
i = i + (i >> 8);
i = i + (i >> 16);
count += i & 0x3f;
return count;
}
function benchmark() {
function compare(a, b) {
if (a[1] > b[1]) {
return -1;
}
if (a[1] < b[1]) {
return 1;
}
return 0;
}
funcs = [
[count1s, 0],
[count1j, 0],
[count1sb, 0],
[countOnes, 0]
];
funcs.forEach((ff) => {
console.log("Benchmarking: " + ff[0].name);
ff[1] = time_function(2500, ff[0], Number.MAX_SAFE_INTEGER);
console.log("Score: " + ff[1]);
})
return funcs.sort(compare);
}
return benchmark;
})()
log("Starting benchmark...\n");
res = benchmark();
console.log("Winner: " + res[0][0].name + " !!!");
count = 1;
res.forEach((r) => {
log((count++) + ". " + r[0].name + " score: " + Math.floor(10000 * r[1] / res[0][1]) / 100 + ((count == 2) ? "% *winner*" : "% speed of winner.") + " (" + Math.round(r[1] * 100) / 100 + ")");
});
log("\nWinner code:\n");
log(res[0][0].toString());
<textarea cols=80 rows=30 id="log"></textarea>
The benchmark will run for 10s.
Doing n = n & (n - 1) you removing last 1 bit in the number.
According to this, you can use the following algorithm:
function getBitCount(n) {
var tmp = n;
var count = 0;
while (tmp > 0) {
tmp = tmp & (tmp - 1);
count++;
}
return count;
}
console.log(getBitCount(Math.pow(2, 10) -1));
Given that you're creating, sorting, and joining an array, if it's literally faster you want, you're probably better off doing it the boring way:
console.log(countOnes(8823475632));
function countOnes(i) {
var str = i.toString(2);
var n;
var count = 0;
for (n = 0; n < str.length; ++n) {
if (str[n] === "1") {
++count;
}
}
return count;
}
(Use str.charAt(n) instead of str[n] if you need to support obsolete browsers.)
It's not as l33t or concise, but I bet it's faster it's much faster:
...and similarly on Firefox, IE11 (IE11 to a lesser degree).
Below works fine with any number:
var i=8823475632,count=0;while (i=Math.floor(i)) i&1?count++:0,i/=2
console.log(count); //17
change the i to the value you want or wrap it as a function
if integer is within 32-bit , below works
var i=10,count=0;while (i) i&1?count++:0,i>>=1
if you want to use an absolute one liner solution you can have a look at this.
countBits = n => n.toString(2).split('0').join('').length;
1.Here n.toString(2) converts n into binary string
2.split('0') makes array out of the binary string splitting only at
0's and hence returning an array of only 1 present in binary of n
3.join('') joins all one and making a string of 1s
4.length finds length of the string actually counting number of 1's in n.
A few more "fun" 1-liners:
Recursive: count each bit recursively until there's no more bits set
let f = x => !x ? 0 : (x & 1) + f(x >>= 1);
Functional: split the base 2 string of x and return the accumulated length of bits set
g = x => x.toString(2).split('0').map(bits => bits.length).reduce((a, b) => a + b);
Keeps on checking if last bit is 1, and then removing it. If it finds last bit is one, it adds it to its result.
Math.popcount = function (n) {
let result = 0;
while (n) {
result += n % 2;
n = n >>> 1;
};
return result;
};
console.log(Math.popcount(0b1010));
For 64 bits, you can represent the number as two integers, the first is the top 32 digits, and the second is the bottom 32. To count number of ones in 64 bits, you can seperate them into 2, 32 bit integers, and add the popcount of the first and second.
You can skip Number, sort and the second toString. Use filter to only consider the 1s (a truthy value) in the array then retrieve how many got through with length.
i.toString(2).split('').filter(v => +v).length
Simple solution if you just want to count number of bit!
const integer = Number.MAX_SAFE_INTEGER;
integer.toString(2).split("").reduce((acc,val)=>parseInt(acc)+parseInt(val),0);
Regex
const bitCount = (n) => (n.toString(2).match(/1/g) || []).length;
Bitwise AND, Right Shift
function bitCount(n) {
let count = 0;
while(n) {
count += n & 1;
n >>= 1;
}
return count;
}
Brian Kernighan algorithm
function bitCount(n) {
let count = 0;
while(n) {
n &= (n-1);
count ++;
}
return count;
}
test:
bitCount(0) // 0
bitCount(1) // 1
bitCount(2) // 1
bitCount(3) // 2
I am pretty new to programming, I am getting to know JavaScript, and I've just learned the notion of recursion. Now I am given a problem, to create a function (like const f = function(n) { }) and if we if we call the function with f(5), we should see:
*
***
*****
*******
*********
The number of vertical stars must be determined by the input.
I've got to use no for/while/do-while; recursion only to loop.
I've come up with this code to concatenate 5 stars
const f = function(n) {
if (n === 0) {
return "";
}
return "*" + f(n - 1);
};
console.log(f(5));
Though, I don't see how to make the triangle, what can I do?
You can use this code:
const f = function(chr, n) {
if (n === 0) {
return "";
}
return chr + f(chr, n - 1);
};
const g = function(max) {
const inner = function(n) {
if (n > 1) {
inner(n-1);
}
console.log(f(' ', max-n) + f('*', (n*2)-1));
};
inner(max);
};
g(5);
Please see and play the example below
function pyramid(n, row = 0, level = '') {
if (row === n) {
return
}
if (level.length === n * 2 - 1) {
console.log(level)
return pyramid(n, row + 1)
}
const midpoint = Math.floor((2 * n - 1) / 2)
let add;
if (midpoint - row <= level.length && midpoint + row >= level.length) {
add = '#'
} else {
add = ' '
}
pyramid(n, row, level + add)
}
pyramid(3)
You are on the right way with the
if (n === 0) ...
and the
f(n-1)
these are correct already.
Now you just need to output the correct number of stars in every function call. Look at string.repeat() for that.
If you want to see a complete solution and not just a hint: here is my solution.
function recursiveStar(n){
if(n === 0) return 0;
recursiveStar(n - 1);
return console.log('*'.repeat(n));
}
recursiveStar(5)
You can try this
function pyramid(n, row = 0) {
if (n === row) return;
let midpoint = Math.ceil((n + n - 1) / 2);
let str = "";
for (let j = 1; j <= n + n - 1; j++) {
if (j <= midpoint + row && j >= midpoint - row) str += "#";
else str += " ";
}
console.log(str);
pyramid(n, row + 1);
}
pyramid(5);
As this old question has recently been brought back up, here's an alternative version that treats the depth as constant and recurs on the row number:
const pyramid = (n, r = 0) =>
r > n - 1
? ''
: ' '.repeat ((n - 1 - r)) + '*'.repeat ((2 * r + 1)) + '\n' + pyramid (n, r + 1)
console .log (pyramid (5))
We keep n fixed, starting r at zero, and then recursively incrementing r until it's greater than n. In our base case, when n is 0 and r is 0, we get r > n - 1, and return an empty string. The recursive case proceeds by adding the right number of spaces, the the right number of starts to our first row, and calculating the remaining rows by incrementing r, and recurring.
const pyramid = (n, r = 0) =>
r > n - 1
? ''
: ' ' .repeat ((n - 1 - r)) + '*' .repeat ( (2 * r + 1)) + '\n' + pyramid (n, r + 1)
// `-----------------------' `------------------------' `--' `----------------'
// | | | |
// | | | +-- next rows
// | | +---------------- line break
// | +----------------------------------- asterisks
// +--------------------------------------------------------------- empty spaces
The only slight trickiness in that is the calculation of the correct number of stars and spaces. It's pretty easy to see that for pyramid (5), the spaces should decrease steadily from 4 down to 0 and the asterisks should increase from 1 up to 9. These are captured by the formulas n - 1 - r and 2 * r + 1 respectively.
This was originally for an assignment, and if that assignment didn't allow us to use String.prototype.repeat, then we could easily write our own recursive version in a function like this:
const repeat = (c, n) =>
n < 1 ? '' : c + repeat (c, n - 1)
const pyramid = (n, r = 0) =>
r > n - 1
? ''
: repeat (' ', (n - 1 - r)) + repeat ('*', (2 * r + 1)) + '\n' + pyramid (n, r + 1)
I'm trying to accomplish the following :
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.
Note that inserting the three dots to the end will add to the string length.
However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.
The code I have will only pass test if my num >2, otherwise it fails.
function truncateString(str, num) {
// Clear out that junk in your trunk
var trunString = "";
if (str.length > num) {
trunString = str.slice(0, (num -3)) + "...";
return trunString;
}
return str;
}
truncateString("A-", 1);
Found a solution for this:
var trunString = "";
if (str.length > num && num >= 4 ) {
trunString = str.slice(0, (num - 3)) + "...";
return trunString;
}
else if (str.length > num && num <= 3) {
trunString = str.slice(0, (num)) + "...";
return trunString;
}
You could use a conditional (ternary) operator ?:
function truncateString(str, num) {
return str.length > num ?
str.slice(0, num > 3 ? num - 3 : num) + "..." :
str;
}
console.log(truncateString("Abcdefghijk", 5));
console.log(truncateString("A-", 1));
console.log(truncateString("Alpha", 5));
console.log(truncateString("Beta", 5));
console.log(truncateString("Epsilon", 3));
function truncateString(str, num) {
// clear out that junk in your trunk
var trunString = '';
if (str.length && str.length > num) {
trunString = str.slice(0, num - 1) + '…';
return trunString;
}
return str;
}
truncateString("A-", 1);
P.S.: read also #techfoobar comment, using &hellip HTML entity will help...
This is my solution, hoping to help you
function truncate(str, num) {
// Clear out that junk in your trunk
var newStr;
if(num>3&&num<str.length){
newStr=str.slice(0,num-3)+"...";
}else if(num>=str.length){
newStr=str;
}else{
newStr=str.slice(0,num)+"...";
}
return newStr;
}
truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);