Javascript: How do I replace text between two delimiters? - javascript

var record = "HENRY|5|58|L581"
How do I change the above to:
record now equals "HENRY|Five|58|L581"
I know how to retrieve the index of the first '|' and the second '|' .. I know how to retrieve the number '5' into a string.
But I have no idea how to actually replace that 5 with the word Five.
The part |5| could be any number from 1-50

Something like that ?
record = record.replace('|5|', '|FIVE|');
Following edit :
To replace any number by FIVE, you can do
record = record.replace(/\|\d+\|/, '|FIVE|');
If you want to replace with something depending of the number (maybe you want TEN when the number is 10), then you'll have to do some work :
record = record.replace(/\|\d+\|/, function(str) {
var number = parseInt(str,10);
return 'FIVE'; // here build a new string and return it
});

You can do this, for example:
var record = "HENRY|5|58|L581"
var recordArray = record.split("|");
for (var i = 0; i < recordArray.length; i++) {
if (recordArray[i] === "5") {
recordArray[i] = "FIVE";
}
}
record = recordArray.join("|"); // or record = recordArray.toString();
Is this what you want to achieve?
UPDATE
If you want any number, you can set it into a function:
function changeNumber(textVar, valueToChange, replaceText) {
var recordArray = textVar.split("|");
for (var i = 0; i < recordArray.length; i++) {
if (recordArray[i] === valueToChange) {
recordArray[i] = replaceText;
}
}
return recordArray.join("|"); // or recordArray.toString();
}
See demo.

I presume you don't want to replace any number with "five", you want to replace with the actual string representing number.
var repl = [0, 1, ....];
var to = ["zero", "one", ...];
var recordArray = record.split("|");
for (var i = 0; i < recordArray.length; i++) {
recordArray[i] = to[indexOf(recordArray[i], repl)];
}
finStr = recordArray.join("|");

Related

JavaScript word guessing game issue: the same letter won't populate multiple times once the letter key is pressed

I have my word guessing game setup so that when the user presses the correct letter, the corresponding underscore is replaced with the letter. However, I can't seem to get the same letter to populate multiple times. Example: the word "Pennywise" has two of the letter 'n', but when the letter is pressed only one 'n' will populate no matter how many times I press the letter.
// Global Variables
// ================================================================================================================================================
// Create an array of words
var word = [
"michael myers",
"freddy krueger",
"jason voorhees",
"xenomorph",
"pinhead",
"ghostface",
"hannibal lector",
"pennywise",
"leatherface",
"chucky",
"jack torrance"
]
var rightLetter = [];
var wrongLetter = [];
var underScore = [];
// Choose word randomly
var randNum = Math.floor(Math.random() * word.length);
var randWord = word[randNum];
console.log(randWord);
// DOM manipulation
var docUnderScore = document.getElementsByClassName("underscore");
var docRightGuess = document.getElementsByClassName("rightGuess");
var docWrongGuess = document.getElementsByClassName("wrongGuess");
// ================================================================================================================================================
// Main
// ================================================================================================================================================
// Create underscore based on length of word
var generateUnderscore = () => {
for ( var i = 0; i < randWord.length; i++) {
underScore.push("_");
}
return underScore;
}
// Get user guess
document.addEventListener("keypress", (event) => {
var keyWord = String.fromCharCode(event.keyCode);
// if user's guess is correct
if (randWord.indexOf(keyWord) === -1) {
// replace underscore with correct letter
underScore[randWord.indexOf(keyWord)] = keyWord;
docUnderScore[0].innerHTML = underScore.join(" ");
// check to see if user word matches guess
if (underScore.join("") === randWord) {
alert("You Survived!");
}
}
// if user's guess is incorrect
else {
wrongLetter.push(keyWord);
docWrongGuess[0].innerHTML = wrongLetter;
}
});
docUnderScore[0].innerHTML = generateUnderscore().join(" ");
The issue is that you won't proceed further using randWord.indexOf(keyWord) as each time it fetches the first occurence of the letter you want to find, instead you could maintain a counter and match letter each time keydown event is fired, if it doe match then increment it to proceed:
// Create an array of words
var word = [
"michael myers",
"freddy krueger",
"jason voorhees",
"xenomorph",
"pinhead",
"ghostface",
"hannibal lector",
"pennywise",
"leatherface",
"chucky",
"jack torrance"
];
var rightLetter = [];
var wrongLetter = [];
var underScore = [];
var counter = 0;
// Choose word randomly
var randNum = Math.floor(Math.random() * word.length);
var randWord = word[7];
console.log(randWord);
// DOM manipulation
var docUnderScore = document.getElementsByClassName("underscore");
var docRightGuess = document.getElementsByClassName("rightGuess");
var docWrongGuess = document.getElementsByClassName("wrongGuess");
// Create underscore based on length of word
var generateUnderscore = () => {
for (var i = 0; i < randWord.length; i++) {
underScore.push("_");
}
return underScore;
}
// Get user guess
document.addEventListener("keypress", (event) => {
var keyWord = String.fromCharCode(event.keyCode);
// if user's guess is correct
if (randWord[counter] == keyWord) {
// replace underscore with correct letter
underScore[counter] = keyWord;
docUnderScore[0].innerHTML = underScore.join(" ");
// check to see if user word matches guess
if (underScore.join("") === randWord) {
console.log("You Survived!");
}
counter++;
}
// if user's guess is incorrect
else {
wrongLetter.push(keyWord);
docWrongGuess[0].innerHTML = wrongLetter;
}
});
docUnderScore[0].innerHTML = generateUnderscore().join(" ");
<div class="underscore"></div>
<div class="rightGuess"></div>
<div class="wrongGuess"></div>

Shift letters from input to the next in alpabhet

I have been searching the web and I have found a few examples about my current problem, and all seems to be addressing the same topic: deciphering text. But I cannot find anything written in javascript. I gave it a shot, but I'm stuck when trying to convert the string in to an array.
Lets say that the current alphabet is
var alpabhet=[
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö'
];
And I have a string ammj, that I enter in the input. Then I want to be able to shift with right and left key and view the output of that current shift. So a shift of two (2) would result in the string cool. And a shift of 5 for the string åjjg would also result in cool.
So my main concern is, how can I convert a user input to an array with javascript?
I have a input filed:<input id="text_to_be_shifted" type="text"> and then I'm trying to loop the input and arrange into a array
var values = {};
var inputs = document.getElementById('text_to_be_shifted');
for( var i = 0; i < inputs.length; i++ ) {
values[inputs[i].name] = inputs[i].value;
}
Have a look at my fiddle: http://jsfiddle.net/p8kqmdL1/
Here you have a live and working example, with a check so that shifting letter 'a' with -1 will convert it to last letter of the alphabet 'ö', -2 to 'ä' e.t.c. and shifting last letter of alphabet with 1 will set it to 'a', with 2 to 'b' e.t.c:
var alpabhet=[
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö'
];
var values = {};
var inputs = document.getElementById('text_to_be_shifted');
for( var i = 0; i < inputs.length; i++ ) {
values[inputs[i].name] = inputs[i].value;
}
function outputText(number){
var newtext = [];
var inputtext = document.getElementById('text_to_be_shifted').value.split('');
inputtext.forEach(letter=> {
var ind_ofLetter = alpabhet.indexOf(letter);
ind_ofLetter = ind_ofLetter + number;
if (ind_ofLetter < 0){
ind_ofLetter = alpabhet.length + ind_ofLetter;
}else if(ind_ofLetter > alpabhet.length-1){
ind_ofLetter = ind_ofLetter - alpabhet.length;
}
newtext.push(alpabhet[ind_ofLetter]);
});
document.getElementsByClassName('output')[0].innerHTML = newtext.join('');
}
function shiftUp() {
var currentShift = document.getElementById('currentShift');
var number = currentShift.innerHTML;
number++;
currentShift.innerHTML = number;
outputText(number);
}
function shiftDown() {
var currentShift = document.getElementById('currentShift');
var number = currentShift.innerHTML;
number--;
currentShift.innerHTML = number;
outputText(number);
}
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '37') {
console.log('left arrow')
shiftDown()
}
else if (e.keyCode == '39') {
console.log('right arrow')
shiftUp()
}
}
<b>Current shift: </b><span id="currentShift">0</span>
<br><input id="text_to_be_shifted" type="text">
<div id='output' class="output"></div>
There is only one input, so there is no point in looping over it.
To get an array, you should use something like:
document.getElementById('text_to_be_shifted').split("");
You can then use the map function to shift the elements
let arr = document.getElementById('text_to_be_shifted').split("");
let shifted = arr.map((c) => alpabhet[(alpabhet.indexOf(c) + 1) % alpabhet.length]).join("");
in your for loop you can utilize the charAt() function to get the individual character at a given index. W3 schools has a good lesson on this function if needed: https://www.w3schools.com/jsref/jsref_charat.asp
var inputArray = [];
var inputs = document.getElemenById('text_to_be_shifted');
for(let i = 0; i < inputs.length; i++){
inputArray[i] = inputs.charAt(i);
}
Something like this should work to get you an array with a single letter at each index.

Filter table data from first character of string in jquery/javascript

I have a table with some records and a textbox. I want to filter table data based on string entered in textbox on keyup event.
Currently I am using a code block which filter the table data but it search the record in table which exist anywhere in the string.
For example:- If I enter 'ab' in textbox it filter the table record with strings contains the keyword 'ab' like abcd, babd, cdab etc.
But my requirement is when I enter the keyword 'ab' in textbox it search only those string which starts from 'ab' like abcd, abdc etc.
Here is my current code:-
function Search_Gridview(strKey, strGV) {
var strData = strKey.value.toLowerCase().split(" ");
var tblData = document.getElementById(strGV);
var rowData;
for (var i = 1; i < tblData.rows.length; i++) {
rowData = tblData.rows[i].cells[3].innerHTML;
var styleDisplay = 'none';
for (var j = 0; j < strData.length; j++) {
if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
styleDisplay = '';
else {
styleDisplay = 'none';
break;
}
}
tblData.rows[i].style.display = styleDisplay;
}
}
Please help guys......
You can filter with jQuery the columns that contain a string beginning with e.g. "ab" of this way:
var re = $("#TABLE_ID td").filter(function(i){ return this.innerHTML.startsWith("ab") })
//You can after, get the values of each td of the result of this way
re.map(function(i){return this.innerHTML})
You can use RegExp's test method.
var stringData = [
'aaa', 'aab', 'aac',
'aba', 'abb', 'abc'
];
var searchPrefix = 'ab';
var result = stringData.filter(function (str) {
// return true if str has prefix with searchPrefix.
return (new RegExp('^' + searchPrefix)).test(str);
});
console.log(result);
JavaScript Regexp Reference
This appears the most elegant solution.
To change search behavior from "exists anywhere in the data" into "data starts with ". You only need to change one single character, on one single line of your original code and nothing more.
Change this line from this..
if (rowData.toLowerCase().indexOf(strData[j]) >= 0)
into this...
if (rowData.toLowerCase().indexOf(strData[j]) == 0)
What it does is forces the indexOf() to address zero, instead of allowing mid-string matches.
Below is the whole (already modified) code for copy and paste into a project, such as a html table filter.
function Search_Gridview(strKey, strGV) {
var strData = strKey.value.toLowerCase().split(" ");
var tblData = document.getElementById(strGV);
var rowData;
for (var i = 1; i < tblData.rows.length; i++) {
rowData = tblData.rows[i].cells[3].innerHTML;
var styleDisplay = 'none';
for (var j = 0; j < strData.length; j++) {
if (rowData.toLowerCase().indexOf(strData[j]) == 0)
styleDisplay = '';
else {
styleDisplay = 'none';
break;
}
}
tblData.rows[i].style.display = styleDisplay;
}
}
Search_Gridview() = the function's name.
strKey = input search characters
strGV = ID of html table></table

Array text to numbers, find matching values and sort

I have an array which looks like this:
["1,8", "4,6,8", "8,9", "6,9"]
1/ I would like to turn it in to this
[1,8,4,6,8,8,9,6,9]
2/ I would then like to find matching values, by looking for the most number:
[8]
This first has been solved with this:
var carArray = ["1,8", "4,6,8,7,7,7,7", "8,9", "6,9"];
//1) create single array
var arr = carArray.join().split(',');
//2) find most occurring
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
if(maxOccurring){
//there was an element more than once, maxOccuring contains that element
setResult('Most occuring: ' + maxOccurring + ' (' + max + ' times)');
}
else{
//3)/4) ???
setResult('sorting?');
}
//below is only for test display purposes
function setResult(res){
console.log(res);
}
3/ If the are no matching values like this
[1,8,4,6,5,7]
4/ Then I need to compare this array to another array, such as this
[6,7,4,1,2,8,9,5]
If the first number in <4> array above appears in <3> array, then get that number, ie in the above example I need to get 6. The <4> array will be static values and not change. The numbers is <3> will be dynamic.
EDIT Not the most elegant of answers, but I do have something working now. I didn't compare the original array directly with the second array, instead used simple if/else statements to do what I needed:
var carArray = ["1,5", "4", "8,2", "3,9,1,1,1"];
//1) create single array
var arr = carArray.join().split(',');
//2) find most occurring
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
if(maxOccurring){
//there was an element more than once, maxOccuring contains that element
console.log('Most occuring: ' + maxOccurring + ' (' + max + ' times)');
console.log(maxOccurring);
}
else {
// If not occuring, match from a list
if(jQuery.inArray("6", arr) !== -1) { console.log('6'); }
else if(jQuery.inArray("9", arr) !== -1) { console.log('9'); }
else if(jQuery.inArray("7", arr) !== -1) { console.log('7'); }
else if(jQuery.inArray("5", arr) !== -1) { console.log('5'); }
else if(jQuery.inArray("4", arr) !== -1) { console.log('4'); }
else if(jQuery.inArray("1", arr) !== -1) { console.log('1'); }
else { console.log('not found'); }
}
Example Fiddle
Step 1 is fairly easy by using javascript's join and split methods respectively:
var arr = carArray .join().split(',');
For step 2, several methods can be used, the most common one using an object and using the elements themselves as properties. Since you only need to get the most occurring value if there is a reoccurring value, it can be used in the same loop:
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
After the above, the variable maxOccurring will contain the reoccurring value (if any) and max will contain the times it occured
For step 4 the easiest way is to loop through the compare array and get the element that occurs in the input array:
var cmpArr = ['6','7','4','1','2','8','9','5'];
//find the first occurrence inside the cmpArr
res = function(){ for(var i= 0 ; i < cmpArr.length; i++){ if(arr.indexOf(cmpArr[i]) !== -1)return cmpArr[i];}}();
The above uses an in place function which is called immediately to be able to use return. You could also just use a loop and assign res when found, then break from the loop.
Last update, an alternate fiddle where the above is converted to a single function: http://jsfiddle.net/v9hhsdny/5/
Well first of all the following code results in four matching answers since the jQuery selectors are the same.
var questionAnswer1 = $(this).find('input[name=questionText]').val();
var questionAnswer2 = $(this).find('input[name=questionText]').val();
var questionAnswer3 = $(this).find('input[name=questionText]').val();
var questionAnswer4 = $(this).find('input[name=questionText]').val();
var carArray = [questionAnswer1, questionAnswer2, questionAnswer3, questionAnswer4];
You could use the eq(index) method of jQuery to select the appropriate element. However having 4 inputs with the same name is a bad practice.
Well lets say that the carArray has 4 different values which all consist out of comma separated numbers. You could then do the following:
var newArr = [];
carArray.forEach(function(e) {
e.split(",").forEach(function(n) {
newArr.push(n);
});
});
Well then we got to find the most occurring number. JavaScript doesn't have any functions for that so we will have to find an algorithm for that. I found the following algorithm on this stackoverflow page
var count = function(ary, classifier) {
return ary.reduce(function(counter, item) {
var p = (classifier || String)(item);
counter[p] = counter.hasOwnProperty(p) ? counter[p] + 1 : 1;
return counter;
}, {})
}
var occurances = count(newArr);
It isn't clear to me what you're trying to do in step 3 and 4, so can't answer those at the moment.
var ary = ["1,8", "4,6,8", "8,9", "6,9"];
var splitted = ary.reduce(function(acc, item) {
return acc.concat(item.split(','));
}, []);
var occurences = splitted.reduce(function(acc, item) {
if (!acc.hasOwnProperty(item)) acc[item] = 0;
acc[item] += 1;
return acc;
},{}),
biggest = Object.keys(occurences).reduce(function (acc, key) {
if (occurences[key] > acc.occurences) {
acc.name = key;
acc.occurences = occurences[key];
}
return acc;
},{'name':'none','occurences':0}).name;
var vals=["1,8", "4,6,8", "8,9", "6,9"];
// 1) turn into number array
var arrNew=[];
for(var i=0; i<vals.length; i++)
{
arrLine=vals[i].split(",");
for (var j=0;j<arrLine.length;j++) { arrNew.push (parseInt(arrLine[j])) }
}
//result:
alert(arrNew.join(";");
// 2) find most common
var found=[];
for(var i=0; i<arrNew.length; i++) {
// make an array of the number of occurrances of each value
if (found["num"+newArray[i]]) {
found["num"+newArray[i]] ++ ;
} else {
found["num"+newArray[i]]=1;
}
}
var mostCommon={count:0,val:"ROGUE"};
for (x in found) {
if (found[x] > mostCommon.count) {
mostCommon.count=found[x].count;
mostCommon.val=x;
}
}
// result :
alert(mostCommon.val);
//3) not quite sure what you meant there
// 4) unique values:
// at this point the 'found' list contains unique vals
var arrUnique=[];
for (x in found) {
arrUnique.push[x];
}
// result :
alert(arrUnique.join(";"))
//sort:
arrUnique.sort(function(a, b){return a-b});
(This won't work in most browsers) but on a side note, when ES6 becomes widely supported, your solution could look like this:
var arr1 = ["1,8", "4,6,8", "8,9", "6,9"];
var arr2 = arr1.join().split(',');
var s = Array.from(new Set(arr2)); //Array populated by unique values, ["1", "8", "4", "6", "9"]
Thought you might like to see a glimpse of the future!
1.
var orgArray = ['1,8', '4,6,8', '8,9', '6,9'];
var newArray = [];
for (var i in orgArray) {
var tmpArray = orgArray[i].split(',');
for (var j in tmpArray) {
newArray.push(Number(tmpArray[j]));
}
}
2.
var counts = {};
var most = null;
for (var i in newArray) {
var num = newArray[i];
if (typeof counts[num] === 'undefined') {
counts[num] = 1;
} else {
++(counts[num]);
}
if (most == null || counts[num] > counts[most]) {
most = num;
} else if (most != null && counts[num] === counts[most]) {
most = null;
}
}
I don't understand the question 3 and 4 (what "unique order" means) so I can't answer those questions.

Measuring the length of an array, not characters in JavaScript

I have one problem regarding the length function. The problem is that when more than one entries come in the variable, it calculates the length perfectly but for a single entry, it starts to measure the number of characters instead of the amount of elements in the array. I want the alert to show 1 because there is one entry: finance consultant, but it's showing 18.
var win = Titanium.UI.createWindow({ backgroundColor:'#FFF' });
var tableView1=Titanium.UI.createTableView();
var Row = [];
Titanium.Yahoo.yql(
'select * from html where url="http://..." '
+ 'and xpath="//tbody/tr/td/a/font" limit 25',
function(e) {
results = e.data.font;
results = results.content;
alert(results.length);
for (var i = 0; i < results.length; i++) {
var rss = results[i];
var rssRow = Titanium.UI.createTableViewRow({ ... });
var titleLabel = Titanium.UI.createLabel({ ... });
rssRow.add(titleLabel);
Row.push(rssRow);
};
tableView1.setData(Row);
});
win.add(tableView1);
win.open();
You can try the following.
results = results.content;
if (typeof results === 'string')
results = [results];
alert(results.length);
So now you've forced a result that is a string to be an array of one string. The rest of your code should work unmodified.
In the line of slashingweapon answer, but simplified, why don't you just do this:
var length = 0;
if(typeof results === "string") {
length = 1;
} else {
length = results.length;
}
alert(length);
It's simpler and cleaner.

Categories