Bear with me, I'm still kinda new to javascript.. I am trying to sort a list of save-game codes to be sorted by their first parse of the element.
var vList =["1|846|Doc|2|0|false|", "11|203|Derik|7|3|false|", "21|670|Mike|5|5|true|", "13|11|Ron|0|0|false|", "9|1000|Blood|9|9|true|"];
var vParse;
for (i = 0; i < (vParse.length); i++)
var vParse[i]= vList.split('|');
// then somehow sort all the data based on vParse[0]?
I tried a few sorting submissions from other folks, but I couldn't get it to ignore all the font after the first parse. Could anyone help me please?
You can use Array.sort and just split on the pipe, get the first item, and when subtracting the strings are converted to numbers anyway
var vList =["1|846|Doc|2|0|false|", "11|203|Derik|7|3|false|", "21|670|Mike|5|5|true|", "13|11|Ron|0|0|false|", "9|1000|Blood|9|9|true|"];
vList.sort(function(a,b) {
return a.split('|')[0] - b.split('|')[0];
});
console.log(vList)
Try some like that:
vList.sort( function( a, b ) {
return parseInt( a.split( '|' )[ 0 ] ) - parseInt( b.split( '|' )[ 0 ] );
} );
You can read more about sort, split and parseInt methods.
How about this
vList.map(function(el){return {sortBy : parseInt(el.split('|')[0]), original : el}}).sort(function(a,b){return a.sortBy - b.sortBy}).map(function(el){return el.original})
Related
I have posted a few days ago a question about a challenge that I did on jshero.net:
The link for the question is [here]
I feel like I am close, the best answer I could come up with is:
function list(arr){
let myL= arr.slice(0,2);
let myLi= myL.join(' , ')
let myL2 = arr.slice(2,3);
let myLi2= myL2.join(' and ');
let myList = myLi + myLi2
if (arr.length <=2){
return arr.join(' and ')} else {
return myList}
}
list(['Huey', 'Dewey', 'Louie'])
Now if I use this code it will return 'Huey , DeweyLouie'. Do you have any idea on how to get the right answer?
Just slice to the last element, and then append last element. Add a few checks for edge cases.
Seems like a really trivial problem. Not sure why all the overly complicated solutions. Am I misunderstanding the problem?
list = l => l.length > 1 ? l.slice(0,-1).join(', ') + ` and ${l.pop()}` : l[0]||''
console.log(
list(['Huey','Dewey','Louie'])
)
console.log(
list(['Huey','Dewey'])
)
console.log(
list(['Huey'])
)
I have a variable which for example returns: +category1+author3
In my HTML I have 2 links: one with class="category1" and one with class="author3".
I want to hide both links (with for example $("a.category1").fadeOut(); ), but therefore I need to filter the words category1 and author3 out of the variable above. So I want to remove the divider + and let the code read that after every + comes a new word, which I want to use as a class in the further code.
So basically I want to get this +category1+author3 to two variables category1 and author3 with the + as divider using Javascript (there also can be 1 ore 3 or 4 words in the variable, like: +category1+category4+author3+genre2).
Use a string.split method on +
var s="+category1+author3";
classSplit=s.split("+"); //This will be an array
console.log(classSplit[1]) //category1
console.log(classSplit[2]) //author3
//console.log(classSplit[0]) This will be blank/null as there is nothing before+
EDIT:
for(i=1;i< classSplit.length;i++) {//Start with 1 as 0 is null
$classSplit[i].fadeOut();
}
try this
var classSelector = "+category1+author3".split("+").join(",.");
$( classSelector ).fadeOut();
or
var classSelector = "+category1+author3".replace(/\+/g, ", .");
$( classSelector ).fadeOut();
i am relatively new to stackoverflow and have searched for some time for an answer to my question. I found some links like this one How to split a comma-separated string?
but still can't quite understand what I am doing wrong with my short little javascript program.
Anyway here it is. Help would be appreciated.
I basically am trying to create a prompt that asks the user to input 3 numbers seperated by commas, then change that string into an array so that I can multiply the values later on. So far, when i try to console.log this my results are as follows : 1,2
It doesn't print out the third digit(3rd number entered by the user).
var entry = prompt("Triangle side lengths in cm (number,number,number):")
if(entry!=null && entry!="") {
entryArray = entry.split(",");
for (i=0; i<3; i++)
{
entryArray[i] = entry.charAt([i]);
}
}
console.log(entryArray[0] + entryArray[1] + entryArray[2]);
Split creates an array already. So, if you enter 1,2,3, you get an array like this when you split it: ["1", "2", "3"]. In your for loop, you are getting the characters from the original input, not your array. In order to add them, you need to change the input to numbers since they are considered strings. So your for loop should look like this:
for (i=0; i<3; i++)
{
entryArray[i] = parseFloat(entryArray[i]);
}
overwriting the strings with the digits.
Try
for (i=0; i<3; i++)
{
entryArray.push(parseInt(entryArray[i]);
}
You can remove the body of the for。like this:
var entry = prompt("Triangle side lengths in cm (number,number,number):")
console.log(entry);
if(entry!=null && entry!="") {
entryArray = entry.split(",");
console.log(entryArray);
}
console.log(entryArray[0] + entryArray[1] + entryArray[2]);
try this code, i removed your looping which was overwriting the array.
var entry = prompt("Triangle side lengths in cm (number,number,number):");
if(entry!=null && entry!="") {
entryArray = entry.split(",");
console.log(entryArray[0] + entryArray[1] + entryArray[2]);
}
I am trying to sum up the following numbers.
var number1= 12,000.00 ; var number2= 12,000.00;
I have tried this alert(number1+number2); but it doesn't return any data.
Could you please help me to solve this problem?
Thanks
The code in your question is invalid javascript. You can't have a , inside a numeric literal. You have to store it as a string, then parse it manually:
var number1 = '12,000.00';
var number2 = '12,000.00';
function parseCurrency( num ) {
return parseFloat( num.replace( /,/g, '') );
}
alert( parseCurrency(number1) + parseCurrency(number2) );
This won't work. Use accounting.js's unformat function to parse 12,000 as a string instead.
I am sure that this will be a simple solution for someone well versed in jquery.
I am wanting to pass the path name into the if statement so
http://address.com/catalog/product <= catalog then gets passed into the if statment.
if (/\/catalog\//.test(window.location)) {
jQuery('#name-div').hide();
}
so it hides a div if its a child of http://address.com/catalog
var url = location.pathname.split("/")[1];
if (/\/url\//.test(window.location)) {
jQuery('#name-div').hide();
}
As I read your question, what you need is to create a RegExp object from a string since your want to pad with / characters:
var url = location.pathname.split("/")[1],
re = new RegExp('/' + url + '/'); // Create RegExp object from padded string
if (re.test(window.location)) {
jQuery('#name-div').hide();
}
You basically have the answer to your own problem.
$(function(){
var url = window.location.href;
if( /catalog/i.test(url) )
$('#name-div').hide();
});
Unless you have other URLS with catalog in them, there's no reason to dissect the URL any further. Just make sure you select your element after the DOM is ready as I did in my example.
you can do it in many way like:
if( window.location.indexOf(url) !== -1 )
or
if( window.location.search( /url/ig ) )
or
if( window.location.match( /url/ig ).length > 0 )
In this examples you don't even need to use jQuery. It is just normal javascript.