How to use a for loop and splice to remove a word and then check an array for a specific word
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.
var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]
function removeElement (aTable, aName) {
for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {
aTable.splice(i, 1)
document.write(aTable); {break;}
} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}
I've tried to solve it this way, but I don't get it right. The output should be something like this:
Anne, Kari, Marit, Ingrid
Victoria is not in the list
javascript jquery html arrays string
add a comment |
I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.
var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]
function removeElement (aTable, aName) {
for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {
aTable.splice(i, 1)
document.write(aTable); {break;}
} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}
I've tried to solve it this way, but I don't get it right. The output should be something like this:
Anne, Kari, Marit, Ingrid
Victoria is not in the list
javascript jquery html arrays string
why isbreak
wrapped in a block?
– Nina Scholz
Nov 16 '18 at 20:50
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59
add a comment |
I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.
var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]
function removeElement (aTable, aName) {
for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {
aTable.splice(i, 1)
document.write(aTable); {break;}
} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}
I've tried to solve it this way, but I don't get it right. The output should be something like this:
Anne, Kari, Marit, Ingrid
Victoria is not in the list
javascript jquery html arrays string
I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.
var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]
function removeElement (aTable, aName) {
for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {
aTable.splice(i, 1)
document.write(aTable); {break;}
} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}
I've tried to solve it this way, but I don't get it right. The output should be something like this:
Anne, Kari, Marit, Ingrid
Victoria is not in the list
javascript jquery html arrays string
javascript jquery html arrays string
edited Mar 8 at 1:27
Jack Bashford
17.1k51849
17.1k51849
asked Nov 16 '18 at 20:47
SenselessSenseless
427
427
why isbreak
wrapped in a block?
– Nina Scholz
Nov 16 '18 at 20:50
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59
add a comment |
why isbreak
wrapped in a block?
– Nina Scholz
Nov 16 '18 at 20:50
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59
why is
break
wrapped in a block?– Nina Scholz
Nov 16 '18 at 20:50
why is
break
wrapped in a block?– Nina Scholz
Nov 16 '18 at 20:50
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59
add a comment |
4 Answers
4
active
oldest
votes
Do you have to write functions? Javascript has Array methods to do this for you.
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
add a comment |
The issue is this line:
if (aTable[1] === aName) {
That only checks the second index ("Inger"
). It should be this:
if (aTable[i] === aName) {
add a comment |
You could do this:
function removeElement(aTable, aName) {
const index = aTable.indexOf(aName);
if (index > -1) {
aTable.splice(index, 1);
document.write(aTable);
} else {
document.write(aName + " is not in the list");
}
}
Or you could always use functional Array.prototype.filter().
add a comment |
Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345163%2fhow-to-use-a-for-loop-and-splice-to-remove-a-word-and-then-check-an-array-for-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do you have to write functions? Javascript has Array methods to do this for you.
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
add a comment |
Do you have to write functions? Javascript has Array methods to do this for you.
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
add a comment |
Do you have to write functions? Javascript has Array methods to do this for you.
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
Do you have to write functions? Javascript has Array methods to do this for you.
Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]
femaleName = femaleName.filter(name => name !== 'Inger')
console.log(femaleName);
console.log(femaleName.includes('Inger'));
answered Nov 16 '18 at 20:55
WillWill
1,82911211
1,82911211
add a comment |
add a comment |
The issue is this line:
if (aTable[1] === aName) {
That only checks the second index ("Inger"
). It should be this:
if (aTable[i] === aName) {
add a comment |
The issue is this line:
if (aTable[1] === aName) {
That only checks the second index ("Inger"
). It should be this:
if (aTable[i] === aName) {
add a comment |
The issue is this line:
if (aTable[1] === aName) {
That only checks the second index ("Inger"
). It should be this:
if (aTable[i] === aName) {
The issue is this line:
if (aTable[1] === aName) {
That only checks the second index ("Inger"
). It should be this:
if (aTable[i] === aName) {
answered Nov 16 '18 at 20:50
Jack BashfordJack Bashford
17.1k51849
17.1k51849
add a comment |
add a comment |
You could do this:
function removeElement(aTable, aName) {
const index = aTable.indexOf(aName);
if (index > -1) {
aTable.splice(index, 1);
document.write(aTable);
} else {
document.write(aName + " is not in the list");
}
}
Or you could always use functional Array.prototype.filter().
add a comment |
You could do this:
function removeElement(aTable, aName) {
const index = aTable.indexOf(aName);
if (index > -1) {
aTable.splice(index, 1);
document.write(aTable);
} else {
document.write(aName + " is not in the list");
}
}
Or you could always use functional Array.prototype.filter().
add a comment |
You could do this:
function removeElement(aTable, aName) {
const index = aTable.indexOf(aName);
if (index > -1) {
aTable.splice(index, 1);
document.write(aTable);
} else {
document.write(aName + " is not in the list");
}
}
Or you could always use functional Array.prototype.filter().
You could do this:
function removeElement(aTable, aName) {
const index = aTable.indexOf(aName);
if (index > -1) {
aTable.splice(index, 1);
document.write(aTable);
} else {
document.write(aName + " is not in the list");
}
}
Or you could always use functional Array.prototype.filter().
edited Nov 16 '18 at 23:24
Christopher Bradshaw
95311330
95311330
answered Nov 16 '18 at 21:00
KhaltKhalt
312
312
add a comment |
add a comment |
Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
add a comment |
Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
add a comment |
Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];
function notify(criteria) {
const position = getPosition(criteria);
const removed = removeName(position);
console.log(names);
console.log(`${removed} is not in the list`);
}
function getPosition(criteria) {
return names.findIndex(name => name === criteria);
}
function removeName(position) {
return names.splice(position, 1);
}
notify('Inger');
answered Nov 16 '18 at 21:15
user615274user615274
1,65611423
1,65611423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345163%2fhow-to-use-a-for-loop-and-splice-to-remove-a-word-and-then-check-an-array-for-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why is
break
wrapped in a block?– Nina Scholz
Nov 16 '18 at 20:50
I am missing where you assign the array femaleName to your aTable
– ratmalwer
Nov 16 '18 at 20:59