Remove a given character from a string without using replace()? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Is this the right way to remove a given character from String?
////not using replace method
function removeChar(str1, s) {
let tempĀ  = str1.split('')
let temp2 = []
for (i = 0; i < temp.length; i++) {
if (temp[i] != s) {
temp2.push(temp[i])
}
}
console.log(temp2.join(''));
}
removeChar("Hello","l")

You can do something like this
function removeChar(str1, s) {
return str1.split(s).join('')
}

this is another solution without replace()
const removeChar = (word, letter) => word.split("").filter(v => v !== letter).join("");

Just use the inbuilt replace function of javascript.
newString = oldString.replace(characterToBeReplaced, '');

Related

How do I get results? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am a beginner in JavaScript and I write the following code, I also write console.log(), and I do not get results, can someone help me and explain to me why I can't get results?
function integers() {
let MyArray;
MyArray = [];
for (let i = 2; i <= 10; i++) {
MyArray.push(i);
}
console.log(MyArray);
}
The javascript functions are not being called automatically. If you need to do so, You can create an auto call function - https://stackoverflow.com/a/10704006/7078456
Or you can manually call your function to get the output of your function.
function integers() {
let myArray = [];
for (let i = 2; i <= 10; i++) {
myArray.push(i);
}
console.log(myArray);
}
integers()

How to search in localstorage for specific word and get entire key and value? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
On click i created localstorage item "storageKey__list" and value is "example.com"
I need to get all item keys and values ends with "__list" and then get entire key and value.
Result must be:
"storageKey__list , example.com"
You could grab the entries in localStorage using Object.entries() and then use .filter() to obtain only the entires ending in "__list" by using .endsWith():
Object.entries(localStorage).filter(([key]) => key.endsWith('__list'));
Output:
[["storageKey__list", "example.com"]]
A more browser friendly version of the code above could be to use the following:
Object.keys(localStorage).filter(function(key) {
return /__list$/.test(key);
}).map(function(key) {
return [key, localStorage.getItem(key)];
});
Output:
[["storageKey__list", "example.com"]]
Try this:
/** #type {[string, string][]} */
const keyValuePairs = [];
for (let i = 0, l = localStorage.length; i < l; i++) {
const key = localStorage.key(i);
if (key.endsWith("__list"))
keyValuePairs.push([ key, localStorage.getItem(key) ]);
}
console.log(keyValuePairs);

I need to sort it out this quiz [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
my code is not working at all
I need to solve this quiz
question is write convertToString as function !
this function should convert to string from parameter
ex )
let output = convertToString(120);
console.log(output); // --> '120'
let output2 = convertToString('hello');
console.log(output2); // --> 'hello'
let output3 = convertToString(true);
console.log(output3); // --> 'true'
this is what I wrote
function convertToString(anything) {
if (typeof anything === 'number' && typeof anything === 'boolean') {
let ret = anything.toString()
} else {
return anything;
}
return ret1;
}
convertToString(120);
The easiest way to convert anything is by making + operation with ""
function convertToString(anything) {
return "" + anything
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
console.log(convertToString(null));
console.log(convertToString(undefined));
Zero checks necessary.
function convertToString(val) {
return String(val);
// or return val.toString();
// or return '' + val;
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));

How to declare a json object or array collection with variable size in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to create an array or json that can be filled when detects the counter like an auxiliarJson with variable size but i dont know how can i do it
TypeError: lAttrsPorDia is undefined
lAttrsPorDia[j] = __oATTRS[i];
var lAttrsPorDia;
var j = 0;
for (var i = 0; i < __oATTRS.length; i++) {
if (__oATTRS[i].Dia == counter) {
lAttrsPorDia[j] = __oATTRS[i];
j++;
alert(JSON.stringify(lAttrsPorDia));
}
}
JavaScript arrays already do have variable size:
var arr = [];
arr.push('Hello');

How to improve selection sort? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is more of an academic/homework question?
Would it be better to change
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
to
$P.swap(arr, index_outer, index_min);
and always swap, as this is the special case when index_outer does have the smallest value? It would be a swap that does nothing, but at the same time it would not break anything. Because this does not happen often I suppose, it would reduce the amount of times an if check was used.
$P.swap = function (arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
$P.selectionSort = function (arr) {
var index_outer,
index_inner,
index_min,
length = arr.length;
for (index_outer = 0; index_outer < length; index_outer++) {
index_min = index_outer;
for (index_inner = index_outer + 1; index_inner < length; index_inner++) {
if (arr[index_inner] < arr[index_min]) {
index_min = index_inner;
}
}
if (index_outer !== index_min) {
$P.swap(arr, index_outer, index_min);
}
}
return arr;
};
I don't think it would always be a good idea. What if array was partially/fully sorted, you would be wasting a call to $P.swap().
As for improving selection sort try sorting the array from both ends simultaneously by taking index_min and index_max. Though number of comparisons would remain same, number of passes would decrease hence decreasing total run time.

Categories