Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My programming lab problem statement:
Design an algorithm that will prompt for and receive an employee number from an operator at a terminal. your program is to search an array of valid employee numbers to check that the employee number is valid, look up a parallel array to retrieve the corresponding employee name for that number, and display the name to the screen. If the employee number is not valid, an error message is to be displayed.
I'm supposed to enter the employee number, and then it displays the employee name. (also need to give an error message if the number is invalid.)
var Emp_num = new Array(123,234,345,456,567,678,789,890,901,012);
var Emp_name = new Array("ED","BOB","LOU","JEAN","MAX","SUE","VIC","TOM","CAL","MO");
var i = 1;
Emp_num=prompt("Enter Employee Number: ");
You can't assign the return value from prompt to Emp_num because that's where you're storing the array of your employee numbers. Create a new variable named Req_num or something and store it there.
Then you'd just convert the user entry to an integer with parseInt and find the indexOf that number in Emp_num. You could then look up this index in Emp_name (if it's greater than -1) to get the employee name, like this:
var Emp_num = new Array(123,234,345,456,567,678,789,890,901,012);
var Emp_name = new Array("ED","BOB","LOU","JEAN","MAX","SUE","VIC","TOM","CAL","MO");
var Req_num = prompt("Enter Employee Number: ");
var Emp_idx = Emp_num.indexOf(parseInt(Req_num, 10));
if (Emp_idx > -1) {
alert("Employee name: " + Emp_name[Emp_idx]);
} else {
alert("Employee number not found.");
}
parseInt docs, indexOf docs
See demo
Related
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
I have a scenario where I have an INPUT, The User enters some text, like for ex: Cat.
I need to search an Array and check if the input from the User is present in my Array.
If it is present I ould want to remove that item from the Array and return a new Array with that item removed.
Suppose I have an arraylist like,
let arrayList = ["cat","mouse","dog"];
User INPUT = cat;
I want to check if the cat is present in arrayList , If yes then I want to remove the cat from the arrayList and want to have a new array that does not contain cat in it. Hence any INPUT provided by User needs to be removed from an array and return me a new array without that item.
You can use filter function to filter the array. Reference link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
function filterArray(arr, searchString) {
var result = arr.filter(function(e) {
return e != searchString;
});
return result;
}
filterArray(arrayList, "cat") // This will return ["mouse", "dog"];
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 6 years ago.
Improve this question
Commonly, arrays are formed like var array = ["some","string","elements"] or var array = [1, 2, 3]. Been searching for times, but I haven't seen articles that may clear my mind. Now, is it possible to have an array structure like var array = [some, string, elements]. As what can be observed, it was somehow a string without "".
To visualize my concern, I have this code
var data = []
for (var x = 0; x < arrayData.length; x++){ //arrData contains [1,2] structure
data.push("$scope.item["arrayData[x]"]");
}
//which gives var data = ["$scope.item[1], $scope.item[2]"]
I needed that var data to form like var data = [$scope.item[1],$scope.item[2]]. Is it possible?
EDIT
My bad, I haven't explained my query fully. The "$scope.item[" is a string, that's why I encapsulated it to ""
EDIT II
Is it possible to have an array structure like var array = [some, string, here]. Consider that some,string and here are not variables.
Don't complecate it, Just go with using JSON.stringify();
Your code should be
data.push(JSON.stringify("$scope.item[" + arrayData[x] + "]"));
You cannot have an array structure like var array = [some, string, elements]. There should be variables named some, string and elements.
$scope is a special variable in AngularJS. So, you should use it without "s. For example:
data.push($scope.item[arrayData[x]]);
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 8 years ago.
Improve this question
JavaScript problem. Can this be done?
I have an input array containing anything between 2 - 5 strings, each with a semi-colon delimited label to identify it. I need to de-duplicate such that the output removes the duplicates but also maintains the string identifiers, grouping if necessary.
Input Array (3 elements)
string1;apple|string2;orange|string3;orange
Output Array (now 2 elements since 'orange' appeared twice)
string1;apple|string2/string3;orange
I don't mind helping people that are just starting with a new programming language or programming: (also a js fiddle)
var arr=["string1;apple","string2;orange","string3;orange"];
var finalArr= [];
var output = {};
for(var i in arr){
var keyVal = arr[i].split(";");
if(output[keyVal[1]]==undefined){
output[keyVal[1]] = [keyVal[0]]
} else {
//should be an array
output[keyVal[1]].push(keyVal[0]);
}
}
for( var i in output){
finalArr.push(output[i].join("/")+";"+i);
}
console.log(finalArr);
I think your best option for this would be to find a way to logically group this information.
Convert the pipe-delimited string into an array.
Iterate through the array
Assign each id/value pair to a property=value pair in a struct.
Strip out the id and delimiter so you're left with the string itself in the array.
Sort the array.
Deduplicate the array.
Iterate through the array.
Iterate through the struct to generate a list of properties which values match the entry.
Unset the properties which values match the entry to reduce time in future iterations.
This is only one way of doing it. I've given you some hints on how you can approach the problem, but it's up to you to code this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm currently working on a HTML5 project and I need to use localstorage.
I want to be able to dynamically remove items from my localstorage. This means that if I have five items, and I want to remove the second item, the third, fourth and fifth item will be automatically allocated to a lower place so that I don't have a gap left (as this is very annoying when I want to print my whole local storage).
HTML5 localStorage aka Web Storage, uses keys to store string values. So you can store objects or arrays by converting them to JSON before storing:
// define an array of objects
var arr = [
{ name : 'first', description : 'first description' },
{ name : 'second', description : 'second description' },
{ name : 'third', description : 'third description' }
];
// store the array to localStorage as a JSON string
localStorage.setItem("data", JSON.stringify(arr));
// retrieve the JSON from localStorage and parse it back to an array
var arr = JSON.parse(localStorage.getItem("data"));
// remove the second object from the array
arr.splice(1, 1);
// let's update the first object too
arr[0].name = 'first name';
// save it back to localStorage
localStorage.setItem("data", JSON.stringify(arr));
if (localStorage.text)
{
var text1 = localStorage.text;
var splitText1 = text1.split(',');
if (splitText1.length > 0)
for (i in splitText1)
{
var div = document.createElement("div");
div.setAttribute("id", "medicament");
div.innerHTML = "<h1>" + splitText1[i] + "</h1>"; document.body.appendChild(div);
}
}
This is the code we use to print things on our screen.
You can use jstorage to solve your problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array in javascript. I need to remove an item from it. I have to iterate over the array and check whether there is a value called 'mastercheck'. If the value is there in array, I have to remove it and get the remaining items. How to do?
Typically my array consists of value like mastercheck,60154,60155....
First use the indexOf method to determine the index of the item with the needed value. Then you can use the splice method to remove the item at found index.
Something like that:
var array = ['mastercheck', '60154', '60155'];
var index = array.indexOf('mastercheck'); // get the index
array.splice(index, 1); // remove the item
var arr = ['mastercheck',60154,60155];
for(var i=0;i<arr.length;i++){
if(arr[i] === 'mastercheck'){
arr.splice(i,1);
}
}
console.log(arr);
Use this code jsFiddle
var arr = ['mastercheck', '60154', '60155'];
var index = arr.indexOf('mastercheck');
arr.splice(index, 1);