var fNum = parseFloat("32.23.45"); results in 32.23 but I need the string from last decimal point: 23.45
For example, the following strings should return the following values:
"12.234.43.234" -> 43.234,
"345.234.32.34" -> 32.34 and
"234.34.34.234w" -> 34.34
A fairly direct solution:
function toFloat(s) {
return parseFloat(s.match(/\d+(\.|$)/g).slice(-2).join('.'));
}
For example:
toFloat("32.23.45") // 23.45
toFloat("12.234.43.234") // 43.234
toFloat("345.234.32.34") // 32.34
toFloat("234.34.34.234w") // 34.34
Update: Here's an alternative version which will more effectively handle strings with non-digits mixed in.
function toFloat(s) {
return parseFloat(s.match(/.*(\.|^)(\d+\.\d+)(\.|$)/)[2]);
}
The following will do exactly what you would like (I'm presuming that the last one should return 34.234, not 34.24).
alert (convText("12.234.43.234"));
alert (convText("345.234.32.34"));
alert (convText("234.34.34.234w"));
function convText(text) {
var offset = text.length - 1;
var finished = false;
var result = '';
var nbrDecimals = 0;
while(!finished && offset > -1) {
if(!isNaN(text[offset]) || text[offset] === '.') {
if(text[offset] === '.') {
nbrDecimals++;
}
if(nbrDecimals > 1) {
finished = true;
} else {
result = text[offset] + result;
}
}
offset--;
}
return result;
}
Related
My function is to either input a Fahrenheit and output the conversion to Celsius or input Celsius and output the conversion to Fahrenheit. My test challenges that I round whatever result to the first decimal, which I've done correctly. However my test says otherwise, I'm using 'Jasmine' to test the code. Here is what I've got.
const ftoc = function(fahr) {
let input = fahr;
if (typeof fahr === 'number'){
let result = (fahr - 32) * 5/9;
if (Number.isInteger(result) === false) {
return result.toFixed(1);
} else {return result}
}
}
const ctof = function(celc) {
let input = celc;
if (typeof input === 'number') {
let result = celc * (9/5) + 32;
if (Number.isInteger(result) === false) {
return result.toFixed(1);
} else {return result}
}
}
module.exports = {
ftoc,
ctof
}
and here are the tests
const {ftoc, ctof} = require('./tempConversion')
describe('ftoc', function() {
it('works', function() {
expect(ftoc(32)).toEqual(0);
});
it('rounds to 1 decimal', function() {
expect(ftoc(100)).toEqual(37.8);
});
it('works with negatives', function() {
expect(ftoc(-100)).toEqual(-73.3);
});
});
describe('ctof', function() {
it('works', function() {
expect(ctof(0)).toEqual(32);
});
it('rounds to 1 decimal', function() {
expect(ctof(73.2)).toEqual(163.8);
});
it('works with negatives', function() {
expect(ctof(-10)).toEqual(14);
});
});
My errors would be as follows:
Expected '163.8' to equal 163.8.
Expected '37.8' to equal 37.8.
Expected '-73.3' to equal 73.3.
Seems to be expecting some sort of extra period after the numerical result and I'm not sure why that is. Thanks!
Your functions are returning a string, so just update your expect to:
expect(ftoc(100)).toEqual("37.8");
And it will work.
The reason is because .toFixed returns a string by default, as documented here.
I am writing code for recursion. And here is my code.
Here, what I am trying to do is, if string has ' then replace it with HTML quotes and calling function recursively until all ' have been replaced.
But this is always returning me false. When I alert var a. If I not use return false then it returns undefined. Any clue what is the wrong here?
var a = replaceqt(" hello's there 'how are you?' ");
console.log(a);
function replaceqt(object) {
var indexc = object.indexOf("'");
var next = object.charAt(indexc + 1);
var prev = object.charAt(indexc - 1);
if (indexc == 0) {
object = object.replace("'", "‘");
} else if (parseInt(prev) >= parseInt(0) && parseInt(prev) <= parseInt(9)) {
object = object.replace("'", "'");
} else if (next == " ") {
object = object.replace("'", "’");
} else if (prev == " ") {
object = object.replace("'", "‘");
} else {
object = object.replace("'", "’");
}
indexc = object.indexOf("'");
if (indexc > -1) {
replaceqt(object);
return false;
} else {
return object;
}
}
Because you are returning false whenever there is a second call. Should return the result of recursive invocation instead.
var a = replaceqt(" hello's there 'how are you?' ");
console.log(a);
function replaceqt(object) {
var indexc = object.indexOf("'");
var next = object.charAt(indexc + 1);
var prev = object.charAt(indexc - 1);
if (indexc == 0) {
object = object.replace("'", "‘");
} else if (parseInt(prev) >= parseInt(0) && parseInt(prev) <= parseInt(9)) {
object = object.replace("'", "'");
} else if (next == " ") {
object = object.replace("'", "’");
} else if (prev == " ") {
object = object.replace("'", "‘");
} else {
object = object.replace("'", "’");
}
indexc = object.indexOf("'");
if (indexc <= -1) {
return object;
}
return replaceqt(object);
}
BTW you don't need parseInt(num) if num is a number say 0 or 9.
You need to replace
if (indexc <= -1){
return object;
}else{
replaceqt(object); return false;
}
with
if (indexc <= -1){
return object;
}else{
return replaceqt(object);
}
In your original code, the return value of replaceqt(object) is discarded when indexc >= 0.
You should try using .split and .join functions to simplify your code.
For a simple find-replace all, you can do this:
var sentence = "I hate spaces."
var charToFind = " ";
var replacement = "-";
var afterSplit = sentence.split(charToFind) // ["I", "hate", "spaces"]
var result = afterSplit.join(replacement) // "I-hate-spaces"
Your example is more complex than a find replace, because you need to keep track of left and right quotes.
To get around that, we can figure out if it's even or odd using the index in the array.
var someString = "My 'name' is 'Ryan'... I 'think'."
function replaceQuotesFor (str) {
return str
.split("'")
.map(function (str, index) {
var quote = index % 2 === 1
? '‘'
: '’'
return (index === 0)
? str
: quote + str
})
.join('')
}
console.log('Before:', someString)
console.log('After:', replaceQuotesFor(someString))
I stopped using for loops and modifying indices, because it made debugging frustrating.
I hope these functions help simplify your code and help you in the future!
I have been trying to translate my code from es6 to es5 because of some framework restrictions at my work... Although I have been quite struggling to locate what the problem is. For some reason the code does not work quite the same, and there is no errors either ...
Can someone tell me If I have translated properly ?
This is the ES6 code :
function filterFunction(items, filters, stringFields = ['Title', 'Description'], angular = false) {
// Filter by the keys of the filters parameter
const filterKeys = Object.keys(filters);
// Set up a mutable filtered object with items
let filtered;
// Angular doesn't like deep clones... *sigh*
if (angular) {
filtered = items;
} else {
filtered = _.cloneDeep(items);
}
// For each key in the supplied filters
for (let key of filterKeys) {
if (key !== 'TextInput') {
filtered = filtered.filter(item => {
// Make sure we have something to filter by...
if (filters[key].length !== 0) {
return _.intersection(filters[key], item[key]).length >= 1;
}
return true;
});
}
// If we're at TextInput, handle things differently
else if (key === 'TextInput') {
filtered = filtered.filter(item => {
let searchString = "";
// For each field specified in the strings array, build a string to search through
for (let field of stringFields) {
// Handle arrays differently
if (!Array.isArray(item[field])) {
searchString += `${item[field]} `.toLowerCase();
} else {
searchString += item[field].join(' ').toLowerCase();
}
}
// Return the item if the string matches our input
return searchString.indexOf(filters[key].toLowerCase()) !== -1;
});
}
}
return filtered;
}
And this is the code I translated that partially 99% work ..
function filterFunction(items, filters, stringFields, angular) {
// Filter by the keys of the filters parameter
var filterKeys = Object.keys(filters);
// Set up a mutable filtered object with items
var filtered;
// Angular doesn't like deep clones... *sigh*
if (angular) {
filtered = items;
} else {
filtered = _.cloneDeep(items);
}
// For each key in the supplied filters
for (var key = 0 ; key < filterKeys.length ; key ++) {
if (filterKeys[key] !== 'TextInput') {
filtered = filtered.filter( function(item) {
// Make sure we have something to filter by...
if (filters[filterKeys[key]].length !== 0) {
return _.intersection(filters[filterKeys[key]], item[filterKeys[key]]).length >= 1;
}
return true;
});
}
// If we're at TextInput, handle things differently
else if (filterKeys[key] === 'TextInput') {
filtered = filtered.filter(function(item) {
var searchString = "";
// For each field specified in the strings array, build a string to search through
for (var field = 0; field < stringFields.length; field ++) {
// Handle arrays differently
console.log(field);
if (!Array.isArray(item[stringFields[field]])) {
searchString += item[stringFields[field]] + ' '.toLowerCase();
} else {
searchString += item[stringFields[field]].join(' ').toLowerCase();
}
}
// Return the item if the string matches our input
return searchString.indexOf(filters[filterKeys[key]].toLowerCase()) !== -1;
});
}
}
return filtered;
}
These two lines
searchString += `${item[field]} `.toLowerCase();
searchString += item[stringFields[field]] + ' '.toLowerCase();
are not equivalent indeed. To apply the toLowerCase method on all parts of the string, you'll need to wrap the ES5 concatenation in parenthesis:
searchString += (item[stringFields[field]] + ' ').toLowerCase();
or, as blanks cannot be lowercased anyway, just use
searchString += item[stringFields[field]].toLowerCase() + ' ';
Here is a translated code from babeljs itself, as commented above.
'use strict';
function filterFunction(items, filters) {
var stringFields = arguments.length <= 2 || arguments[2] === undefined ? ['Title', 'Description'] : arguments[2];
var angular = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
// Filter by the keys of the filters parameter
var filterKeys = Object.keys(filters);
// Set up a mutable filtered object with items
var filtered = void 0;
// Angular doesn't like deep clones... *sigh*
if (angular) {
filtered = items;
} else {
filtered = _.cloneDeep(items);
}
// For each key in the supplied filters
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
var _loop = function _loop() {
var key = _step.value;
if (key !== 'TextInput') {
filtered = filtered.filter(function (item) {
// Make sure we have something to filter by...
if (filters[key].length !== 0) {
return _.intersection(filters[key], item[key]).length >= 1;
}
return true;
});
}
// If we're at TextInput, handle things differently
else if (key === 'TextInput') {
filtered = filtered.filter(function (item) {
var searchString = "";
// For each field specified in the strings array, build a string to search through
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = stringFields[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var field = _step2.value;
// Handle arrays differently
if (!Array.isArray(item[field])) {
searchString += (item[field] + ' ').toLowerCase();
} else {
searchString += item[field].join(' ').toLowerCase();
}
}
// Return the item if the string matches our input
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return searchString.indexOf(filters[key].toLowerCase()) !== -1;
});
}
};
for (var _iterator = filterKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
_loop();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return filtered;
}
p.s. Or there is a better way to use babeljs directly without manually converting it.
Here is a sample WiFi ssid I have extracted from an Android "wifi config file" (wpa_supplicant.conf).
I'm trying to display all the ssid's in the file, most are okay as they are normal strings wrapped in quotes, for example,
network={
ssid="Linksys"
...
}
However, some entries just wanted to be different and special, for example,
network={
ssid=e299aa20e6b7a1e5ae9ae69c89e98ca2e589a920e299ab
...
}
Now, the question is, how do I convert it back to a readable string (preferably in JS)? I suspect the encoding was wrong (it displays correctly on a native device though.)
Apparently the string is in hex unencoded. By turning it back to binary following by some string manipulation, I am able to encode it back to the readable form.
function HextoUTF8(txt) {
function HexStringToBytes(str) {
if (str.length % 2) throw TypeError("Not a valid length");
return [].map.call(str, function(e) {
return ("000" + parseInt(e, 16).toString(2)).slice(-4);
}).join("").match(/.{8}/g);
}
function BytesToUTF8(bytes) {
var inExpectationMode = false,
itr = new Iterator(bytes),
byte,
availableBitsTable = {
"1": -7,
"2": -5,
"3": -4,
"4": -3
},
expectingBitsLeft = 0,
currectCharacter = "",
result = "";
while (byte = itr.next(), !byte.ended) {
byte = byte.value;
if (inExpectationMode) {
currectCharacter += byte.slice(-6);
} else {
//First in sequence
expectingBitsLeft = determineSequenceLength(byte);
currectCharacter += byte.slice(availableBitsTable[expectingBitsLeft]);
}
inExpectationMode = true;
expectingBitsLeft--;
if (!expectingBitsLeft) {
inExpectationMode = false;
result += String.fromCharCode(parseInt(currectCharacter, 2));
currectCharacter = "";
}
}
return result;
}
function determineSequenceLength(byte) {
if (byte[0] === "0") return 1;
else if (byte.slice(0, 3) === "110") return 2;
else if (byte.slice(0, 4) === "1110") return 3;
else if (byte.slice(0, 5) === "11110") return 4;
}
function Iterator(array) {
if (this === window) throw TypeError("This is a class");
if (!Array.isArray(array)) throw TypeError("An array is required");
this.i = -1;
this.ended = !array.length;
this.array = function() {
return array;
};
}
Iterator.prototype.next = function() {
if (this.ended || ++this.i == this.array().length) {
this.ended = true;
return {
ended: true
};
} else {
return {
ended: this.ended,
value: this.array()[this.i]
};
}
}
return BytesToUTF8(HexStringToBytes(txt));
}
Optimally I should be doing bit manipulation instead, but whatever, it works,
> HextoUTF8("e299aa20e6b7a1e5ae9ae69c89e98ca2e589a920e299ab");
> "♪ 淡定有錢剩 ♫"
Given the following obj:
var inputMapping = {
nonNestedItem: "someItem here",
sections: {
general: "Some general section information"
}
};
I'm writing a function to get that data by passing in a string "nonNestedItem" or in the nested case "sections.general". I'm having to use an eval and I was wondering if there was maybe a better way to do this.
Here is what I have so far and it works okay. But improve!
function getNode(name) {
var n = name.split(".");
if (n.length === 1) {
n = name[0];
} else {
var isValid = true,
evalStr = 'inputMapping';
for (var i=0;i<n.length;i++) {
evalStr += '["'+ n[i] +'"]';
if (eval(evalStr) === undefined) {
isValid = false;
break;
}
}
if (isValid) {
// Do something like return the value
}
}
}
Linky to Jsbin
You can use Array.prototype.reduce function like this
var accessString = "sections.general";
console.log(accessString.split(".").reduce(function(previous, current) {
return previous[current];
}, inputMapping));
Output
Some general section information
If your environment doesn't support reduce, you can use this recursive version
function getNestedItem(currentObject, listOfKeys) {
if (listOfKeys.length === 0 || !currentObject) {
return currentObject;
}
return getNestedItem(currentObject[listOfKeys[0]], listOfKeys.slice(1));
}
console.log(getNestedItem(inputMapping, "sections.general".split(".")));
You don't need to use eval() here. You can just use [] to get values from an object. Use a temp object to hold the current value, then update it each time you need the next key.
function getNode(mapping, name) {
var n = name.split(".");
if (n.length === 1) {
return mapping[name];
} else {
var tmp = mapping;
for (var i = 0; i < n.length; i++) {
tmp = tmp[n[i]];
}
return tmp;
}
}