Javascript array output - javascript

Given the following code I need two things:
This one I know how to do, basically user inputs a number and matching month is provided in an output.
But in the same prompt if user writes a month (for eksample "Aug"), how do I return the index number?
The first part I would solve with a for loop and if/else, but how do I include also the second part with only one prompt from user?
var months = ["Not in use", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"];
var userInput = prompt("Choose a month by number or name!");

You can apply a check on the input value and return response accordingly:
let months = ["Not in use", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"];
let locator = v => Number.isNaN(Number(v)) ? months.indexOf(v) : months[v];
console.log(locator("Aug"));
console.log(locator("3"));

You can check the type of your user prompt with a if/else.
let userInput = prompt('Choose a month by number or name')
let monthNumber = parseInt(userInput)
if (monthNumber === NaN) {
// Do your stuff using userInput as a monthName
} else {
// Do your stuff using monthNumber
}

Instead of an empty first element in the array you should add 1 to the index. Next you should check if the userInput is one of the months and change it to the index of the month + 1.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"]
let userInput = prompt("Choose a month by number or name!");
if (months.include(userInput) {
userInput = months[userInput + 1]
}
In addition you have to turn the user input wich is a string into an integer if it is within the range between 1 and 12
let parsedInt = parseInt(userInput)
if (parsedInt >= 1 && parsedInt <= 12) {
userInput = parsedInt
}

Related

Is there any way to print out an specific element from an array with just its index number?

Well, I want to print out the name of the month from the .getMonth() method.
Keeping in mind that the array index starts from 0, I've made this code:
let date = new Date();
let month = date.getMonth();
month -= 1;
// As array index start from 0 but month from 1, I'm subtracting 1 from it so it too will start from 0.
let months =
[
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
Now I want like if it's May, I will keep the value 4, then subtract 1, which is 3.
The 3rd index in the array months is "May", but how will I ask it to check for the element with its index number?
NOTE : Please don't suggest if statements, because I don't want to use if statements for just displaying elements.
Easy: use the bracket property accessor notation:
let date = new Date
let month = date.getMonth()
let months =
[
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
console.log(months[month])
Arrays are little more than objects with properties named after the positive integers (ie. '0', '1', '2' etc). Because JavaScript is weakly typed you can supply a number (eg. from getMonth!) to the bracket property accessor notation, and the value will be coerced to a string, giving you the result you want.
But, as Jaromanda X points out, the better way is to use the Intl API because this will guarantee accuracy for a given user locale:
const date = new Date
console.log(new Intl.DateTimeFormat('en', { month: 'short'}).format(date))

How to calculate the previous months in JS?

In JavaScript,
How to calculate all the previous months from this month (for a year) in the fastest way?
Say, for input: Jun should expect Jun,May,Apr,...Jan,Dec...Jun
You could follow an approach like this:
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
function getPreviousMonths(currentMonth) {
let index = MONTHS.indexOf(currentMonth);
let result = [];
for(let j = 0; j < MONTHS.length; j++) {
let access = index - j;
if(access < 0) {
access += MONTHS.length;
}
result.push(MONTHS[access]);
}
return result.join(",");
}
I would rather have some mapping, like this.
let months = [Jaunary, February, March, April, May]
Then Having got for example 'March' - find its index in array months, and take previous index or whatever you want

Show items in a box after clicking one by one using Javascript AngularJS

I have written a code, where it will show the next 12 months from current month. Suppose, now is Oct 2015, so it will show all next 12 months, that is upto Oct 2016. I am showing it in a list. But, I want to create a box with right-left arrow enabled. After clicking on left-right arrow it will show next item. After clicking on right arrow it will show next item and after clicking on left arrow it will show previous item. Data items will be shown in a box. Please check my below code, which will print next 12 months from current.
angular.module('picker', []).controller('pickercontroller', function($scope) {
var date = new Date();
var months = [],
monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
for(var i = 0; i <= 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.months = months;
});
<div ng-app="picker">
<div ng-controller="pickercontroller">
<li ng-repeat="currMonth in months">{{currMonth}}</li>
</div>
</div>
Also, check my fiddle :- http://jsfiddle.net/abhijitloco/cqbqow2L/
Check this code to see how you can change the selected month via ng-click
https://jsfiddle.net/jddg3som/
Controller
angular.module('picker', []).controller('pickerCtrl', function($scope) {
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
for (var i = 0; i < 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.changeMonth = function(steps) {
if ($scope.monthIndex + steps >= 0 &&
$scope.monthIndex + steps <= 11
) {
$scope.monthIndex = $scope.monthIndex + steps;
$scope.monthName = $scope.months[$scope.monthIndex];
}
};
$scope.monthIndex = 0;
$scope.months = months;
$scope.monthName = months[0];
});
View
<div ng-app="picker">
<div ng-controller="pickerCtrl">
<h1> {{monthName}} </h1>
<button ng-click="changeMonth(1);">Next Month</button>
<button ng-click="changeMonth(-1);">Previous Month</button>
<li ng-repeat="currMonth in months">{{currMonth}}</li>
</div>
</div>
Use ng-click to handle button event and for next month check whether next month is available in months array or not. If available then set next month from array.
Same for previous month
angular.module('picker', []).controller('pickercontroller', function($scope) {
var date = new Date();
var months = [],
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
for (var i = 0; i <= 12; i++) {
months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
date.setMonth(date.getMonth() + 1);
}
$scope.months = months;
$scope.currMonth = months[0];
$scope.nextMonth = nextMonth;
$scope.prevMonth = prevMonth;
function prevMonth() {
var index = $scope.months.indexOf($scope.currMonth);
if (index != 0 && index < $scope.months.length) {
$scope.currMonth = $scope.months[index - 1];
}
}
function nextMonth() {
var index = $scope.months.indexOf($scope.currMonth);
if (index < $scope.months.length - 1) {
$scope.currMonth = $scope.months[index + 1];
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="picker">
<div ng-controller="pickercontroller">
<p>{{currMonth}}</p>
<button ng-click="prevMonth()">Previous Month</button>
<button ng-click="nextMonth()">Next Month</button>
</div>
</div>

JavaScript: missing logic when array iterate for missing value

I am trying to fill blank values when no data available for particular months. Here is plunker.. http://plnkr.co/edit/f0IklkUfX8tkRZrn2enx?p=preview
$scope.year = [
{"month":"mar", "val":"23"},
{"month":"feb", "val":"45"},
{"month":"jan", "val":"56"}
];
var total = ["jan", "feb", "mar", "apr", "may", "jun", "aug", "sep", "oct", "nov", "dec"];
for(var i=0; i<total.length; i++){
if($scope.year[i].month === undefined){ //logic here to see absent month.
$scope.year.push(
{
"month":total[i],
"val":"0"
})
}
}
I have created array of default total months items for compare each month item from expected object, if the item is absent in expected object, need to create empty item or with value "0" in expected object itself.
You can do something like following
Js Update
var year = [
{"month":"mar", "val":"23"},
{"month":"feb", "val":"45"},
{"month":"jan", "val":"56"}
];
var total = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
// Current months array
var currentMonth = [];
angular.forEach(year, function(item){
currentMonth.push(item.month);
});
// iterating over months
for(var i=0; i<total.length; i++){
//$scope.year = [];
// checking if month is absent
if(currentMonth.indexOf(total[i]) === -1){ //logic here to see absent month.
year.push(
{
"month":total[i],
"val":"0",
"order" : i
})
} else {
year[currentMonth.indexOf(total[i])].order = i; // adding order
}
}
$scope.year = year;
Markup
<!-- Order by new property order -->
<td ng-repeat="item in year | orderBy: 'order'">
{{item.val}}
</td>
For reference - http://plnkr.co/edit/km6jLQv8wxm1XP8QCxvV?p=preview
year[i] is undefined, so you try get field of object that don't exist.
try it "if($scope.year[i] === undefined)"
Your idea of using orderBy won't work based on month because you aren't looking for an alphabetical sort.
Following creates array of months that exist in the data. Then it loops over all months in year and adds missing data. Finally sort the data based on months indexing
// create array of months available in data
var availableMonths = $scope.year.map(function(item){
return item.month;
});
// loop over all months and if it doesn't exist in data add it
total.forEach(function(mo){
if(availableMonths.indexOf(mo) ===-1 ){
$scope.year.push({"month":mo, "val":"0" })
}
});
// sort data using index of months
$scope.year.sort(function(a,b){
return total.indexOf(a.month) > total.indexOf(b.month);
});
remove the orderBy filter in html since data is already sorted
Steps 2 & 3 above could actually be combined to splice() the array so final order is correct but for ease of understanding I left them as separate operations
DEMO
Try this.
$scope.year = [
{"month":"jan", "val":"56"},
{"month":"feb", "val":"45"},
{"month":"mar", "val":"23"}
];
var total = ["jan", "feb", "mar", "apr", "may", "jun", "aug", "sep", "oct", "nov", "dec"];
for(var i=0; i<total.length; i++){
if(!$scope.year[i] || $scope.year[i].month === undefined){
$scope.year.push(
{
"month":total[i],
"val":"0"
})
}
}

Javascript - Multiple Arrays containing Variables

I'm new to Javascript and finding some difficulty in a task writing a program.
My task will be to write a program that will allow a user to enter their date of birth. The program then proceeds to give the corresponding Chinese Zodiac sign in an image and the number of days the user has been alive.
What will be input?
The user's year of birth (assume a valid 4 digit year will be input)
The user's month of birth (assume user will enter at least the first
three letters of a month name, but this could be longer and could
contain upper case characters, so it could be jan, Jan, january, or
January, or other month names)
The user's date (in the month) of birth (assume a valid date will be
entered)
Constants we will use
Create and appropriately name constants to store the following values:
A string containing month
abbreviations 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'
The number of milliseconds in a day 1000*60*60*24
The cycle of Chinese zodiac 12
The year initialising Chinese Zodiac cycles 1924
My code so far:
var year = prompt('Enter year of birth as a 4 digit integer') // A prompt to enter the year of birth.
var month = prompt('Enter the name of the month of birth') // A prompt to enter the month of birth.
var date = prompt('Enter day of birth as an integer') // A prompt to enter the date of birth.
var month = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
I'm having trouble with the input of the month of birth. I'm trying to write a code to assume user will enter at least the first three letters of a month name, but this could be longer and could contain upper case characters, so it could be jan, Jan, january, or January, or other month names.
Any help will be appreciated!
George
Use .substr() to shorten their input, and .toLowerCase() to convert it to lowercase. Then match it to your array of months (the lowercase version). Here's a bit to help you get started:
var month = prompt('Enter the name of the month of birth');
// Chop everything after the first 3 characters and make it lowercase
month = month.substr(0,3).toLowerCase();
// Store your array in months, differently named than the month input
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
// You can then use array.indexOf() to locate it in the array
// Not available in older browsers though
var pos = months.indexOf(month);
if (pos >= 0) {
// valid month, number is pos
}
P.S. Don't forget the ; at the end of each statement!
Combine all of your possible month strings into one array:
var year = prompt('Enter year of birth as a 4 digit integer') // A prompt to enter the year of birth.
var month = prompt('Enter the name of the month of birth') // A prompt to enter the month of birth.
var date = prompt('Enter day of birth as an integer') // A prompt to enter the date of birth.
var possibleMonths = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
Then, iterate through that one array to check to see if your user entered one of those months:
var isValidMonth = false;
for (var i = 0; i < possibleMonths.length; i++){
if (possibleMonths[i] === month){
isValidMonth = true;
break;
}
}
alert(month + " is a valid month: " + isValidMonth);

Categories

Resources