javascript loop iterating too much - javascript

Trying a fun problem of replacing vowels in a string with the next vowel in line aka a->e, e->i, i->o, o->u, not accounting for "u". Starting with an array instead of a string. My second loop (to iterate over vowel array elements) is ignoring my "j
var vowelChange = function(vowelArray, stringToChange) {
for (var i = 0; i<stringToChange.length; i++) {
for (var j = 0; j<vowelArray.length; j++) {
if (stringToChange[i]===vowelArray[j]) {
var newCharacter = vowelArray[j+1]
stringToChange[i] = newCharacter
i++
}
}
}
return stringToChange
};
I'm using node-debug to set breakpoints in a browser, and j is looping to 5 before starting over at 0. I get the correct output, but j should stop at 4...
EDIT
Can somebody explain how I'm using join incorrectly, because I can't get my function to output a string instead of just an array.
var vowelChange = function(vowelArray, stringToChange) {
for (var i = 0; i<stringToChange.length; i++) {
for (var j = 0; j<vowelArray.length-1; j++) {
if (stringToChange[i]===vowelArray[j]) {
stringToChange[i] = vowelArray[j+1]
break
}
}
}
stringToChange = stringToChange.join('')
return stringToChange
};
var vowels = ['a','e','i','o','u']
var firstName = ['t', 'e', 's', 't']
vowelChange(vowels, firstName)
console.log(firstName)

Assuming vowelArray is 0-indexed...
var vowelChange = function(vowelArray, stringToChange) {
for (var i = 0; i<stringToChange.length; i++) {
for (var j = 0; j<vowelArray.length - 1; j++) {
if (stringToChange[i]===vowelArray[j]) {
stringToChange[i] = vowelArray[j+1];
break;
}
}
}
return stringToChange
};

In JavaScript, strings are immutable objects, which means that the
characters within them may not be changed and that any operations on
strings actually create new strings.
So,if you try to change any index of the string, the original string won't change
node
> str = "hello this is dummy string";
'hello this is dummy string'
> str[0] = "w";
'w'
> str
'hello this is dummy string'
So, stringToChange[i] = vowelArray[j+1]; won't work
Could split the string and then join
var vowelChange = function(vowelArray, stringToChange) {
stringToChange = stringToChange.split('');
for(var i=0; i<stringToChange.length;i++){
for(var j=0;j<vowelArray.length-1;j++){
if(stringToChange[i] == vowelArray[j]){
stringToChange[i] = vowelArray[j+1];
break;
}
}
}
stringToChange = stringToChange.join('');
return stringToChange;
};
Example

Related

Why doesn't modify each char of a string?

I don't understand why the for loop doesn't modify the chars of a string.
this is
function testing (str) {
let other_str = str
for (let i = 0; i < other_str.length; i++) {
other_str[i] = 'g'
}
return other_str;
}
console.log(testing('hey'));
I am aware that i can use other ways but i want to understand this.
Strings are immutable , convert the string to an array, do the modifications and join it back :
function testing(str) {
let other_str = [...str];
for (let i = 0; i < other_str.length; i++) {
other_str[i] = 'g';
}
return other_str.join('');
}
console.log(testing('hey'));

Creating an array without hard-coding

I'm trying to create an array of strings and produce the possibilities by the length of array string. For example:
var someStr = ["a","b","c","d"];
//I want to produce this outcome
a
ab
abc
abcd
b
bc
bcd
c
cd
d
I know I can get the # of possibilities for "a" only by this way:
var numCombinations = 0;
var comboString = '';
var outcome = [];
for(var i = 0; i < someStr.length; i++){
comboString += someStr[i];
outcome[i] = comboString;
numCombinations += i; //# of combinations from above
}
But how would I continue with these variables for the left over possibilities? I've thought of creating nested for-loops again and again but that would eventually lead to the (n)th length with hard-coding. Would there be any method(s) to create this and store all the possibilities to the (n)th length?
Hope this help.
function getComboStringListFromIdx(arr, idx){
var result = [];
var comboString = '';
for(var i=idx; i<arr.length; i++){
comboString += arr[i];
result.push(comboString);
}
return result;
}
var someStr = ['a','b','c','d'];
var outCome = [];
for(var i = 0; i<someStr.length; i++){
outCome = outCome.concat(getComboStringListFromIdx(someStr, i));
}
I will also use nested for-loop ! One is normal looping and other is to skip less than current index from first loop !!
var someStr = ["a","b","c","d"];
for(var i = 0;i < someStr.length;i++) {
output(i);
}
function output(index) {
var str = "";
for(var j in someStr) {
if(j < index) {
continue;
}
str += someStr[j];
console.log(str);
}
}
This solution uses a nested for loop and skips concatenation on the first element of the nested for loop.
var arr = ["a","b","c","d"];
for(var i=0;i<arr.length;i++){
var str = arr[i];
for(var j=i;j<arr.length;j++){
if(i!==j)
str+=arr[j];
console.log(str);
}
}
https://jsfiddle.net/fmy539tj/

a code which reverses null terminated string

I am new to js.
I am trying to write a code which reverses null terminated string.
I tried writing using push and pop.
but i am not getting output, can you tell me what is the problem.
providing code below
var word = "Cell0";
//var char = word.stringCharAT();
for (i=0; i< word.length; i++) {
var pushWord = [];
pushWord.push(word[i]);
for (j=0; j< pushWord.length; j++) {
var reverseWord= [];
reverseWord = pushWord[j].pop;
console.log("reverseWord" + reverseWord);
}
}
Here's a solution.
var word = "Cell0";
var reversed = '';
for (var i = word.length-1; i >= 0; i--) {
reversed += word[i];
}
console.log(reversed);
This loops through the characters of the string in reverse and adds the characters to a new string.
pushWord[j] is not an array, .pop is not called.
Define pushWord and reverseWord arrays outside of for loop, within loop, after word[i] is pushed to pushWord, call .unshift() on reverseWord with pushWord[pushWord.length -1] as parameter.
var word = "Cell0";
var pushWord = [];
var reverseWord = [];
for (let i = 0; i < word.length; i++) {
pushWord.push(word[i]);
reverseWord.unshift(pushWord[pushWord.length - 1]);
}
console.log("reverseWord" + reverseWord);
var originalWord = "Cell0";
var reverseWord = [];
for (let i = 0; i < originalWord.length; i++) {
reverseWord.unshift(originalWord [i]);
}
console.log(reverseWord.join(''));
You don't even need to push.
Another way to achieve the result!

Checking if a string contains any part of an array element

I just started learning javascript and I'm working on small chrome extension that checks a certain website's item listings for any items
that contain keywords provided by the user. I'm looking for some help on a method to compare a string to an array of strings.
So I have these variables:
var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter figure', 'magic', 'sword art'];
I need to check if itemDetails contains any of the strings in trackingList. I know I can use indexOf() like this:
function checkArray(str, arr){
for(var i=0; i < arr.length; i++){
if(str.indexOf(arr[i]) > -1)
return true;
}
return false;
}
checkArray(itemDetails,trackingList); // returns false
However, for strings with multiple words in them, such as 'alter figure', I want checkArray() to return true as long as both of the words in the string appear anywhere in itemDetails. So checkArray() should return true in the example above since both 'alter' and 'figure' are in itemDetails.
The method I'm currently using is to split each element in trackingList:
function splitTrackList(arr){
var newarr = [];
for(var i = 0; i < arr.length; i++){
newarr[i] = arr[i].split(" ");
}
return newarr;
}
trackList = splitTrackList(trackList);
// [['alter', 'figure'], ['magic'], ['sword', 'art']]
And then compare using indexOf():
function newCheckArray(str, arr){
var passed = true;
for(var i=0; i < arr.length; i++){
for(var j=0; j < arr[i].length; j++){
if(str.indexOf(arr[i][j]) == -1){
passed = false;
break;
}
else passed = true;
}
if(passed) //stop loop if match found
break;
}
return passed;
}
newCheckArray(itemDetails,trackingList); //returns true
My method works so far but I'm guessing there is a much faster/efficient way of doing this. Please let me know what you think. Thank you in advance.
I would do something like
https://jsfiddle.net/denov/FXjXq/3/
var arr = [
"cat dog mouse",
"blue yellow green",
"pizza burrito hamburger"
];
function isInString(needle, haystack) {
return new RegExp('\\b' + needle + '\\b').test(haystack);
}
function checkForStringInArray(stringToFind, arr) {
var inString = false;
for(var i=0; i < arr.length; i++){
inString = isInString(stringToFind, arr[i]);
if(inString) break;
}
return inString;
}
var inString = checkForStringInArray('yellow', arr);
You can use regular expressions. I am giving you an example, although it is not the most efficient
function checkArray(str, arr){
for(var i=0; i < arr.length; i++){
if(str.match((".*" + arr[i].trim() + ".*").replace(" ", ".*")))
return true;
}
return false;
}
Here I alter the string "key1 keyn" to ".*key1.keyn." so it could match those keys everywhere in the string. Have in mind that this will match them if they are part of another word. Good luck.
P.S. Learn regular expressions. They are pretty important in almost every language.
This should work :
function checkArray(str, arr){
for (var j = 0; j < arr.length;j++) {
wordtab = splitTrackList(arr[j]);
for(var i=0; i < tab.length; i++){
if(str.indexOf(wordtab[i]) > -1)
return true;
}
}
return false;
}
Here is a solution with lambda expressions:
var itemDetails = '[alter] saber 1/8 figure unopened';
var trackingList = ['alter saber', 'magic', 'sword art'];
var trackingList2 = ['alter was', 'magic', 'sword art'];
if(trackingList.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
console.debug("true");
} else {
console.debug("false")
}
if(trackingList2.map(str => str.split(" ")).filter(arrtemp => arrtemp.filter(strin => itemDetails.indexOf(strin) > -1).length == arrtemp.length).length > 0) {
console.debug("true");
} else {
console.debug("false")
}
https://jsfiddle.net/wrz1m0b5/
The regex solution only works if words are in correct order.

Array length undefined after split

I'd like to split a string ("1,2,3") and return it as an int array so I wrote the following function:
function stringToIntArray(string) {
var split = {};
split = string.split(',');
var selected = {};
for (var i = 0; i <= split.length; i++) {
selected[i] = split[i];
}
return selected;
}
However split.length is always undefinied. Where's my mistake?
var selected = {};
doesn't build an array but an object, which has no length property.
You can fix your code by replacing it with
var selected = [];
If you want to return an array of numbers, you can change your code to
function stringToIntArray(string) {
var split = string.split(',');
var selected = [];
for (var i = 0; i < split.length; i++) {
selected.push(parseInt(split[i], 10));
}
return selected;
}
Note that I replaced <= with < in your loop.
Note also that for modern browsers, you can use the map function to make it simpler :
function stringToIntArray(string) {
return string.split(',').map(function(v){ return parseInt(v, 10) });
}

Categories