New to JavaScript and coding, and working on a Parking Garage program that parks cars and checks which cars are parked.
I am starting with a function parkCar () that receives a car name and pushes it into an array, on the condition that there is enough room, setting a max of 7 on parked cars. I am getting stuck on how to code the function so that I can specify the names of the cars I am parking in the garage.
Right now, the program is successfully adding carName to parkedCars up to the max string length of the array, but when I use the console to check the items in the array I get ["carName," "carName"] etc. rather than ["volvo," "prius"] etc. How can I create a variable that allows for unique input of car names into the array?
Code pasted below:
console.log('Enter CarName to park in Anne Lot');
var carName;
const maxCars=7
var parkedCars=[];
var carName
parkCar=function (){
if(parkedCars.length<=maxCars){
parkedCars.push('');
console.log(parkedCars);
console.log("Welcome to Anne's Lot.");
}
else {
console.log("Sorry, Anne's Lot is at max capacity.");
}
}
parkCar=function (cName){ // accept car name value
if(parkedCars.length<=maxCars){
parkedCars.push(cName); // push name of car here.
console.log(parkedCars);
console.log("Welcome to Anne's Lot.");
}
else {
console.log("Sorry, Anne's Lot is at max capacity.");
}
}
console.log('Enter CarName to park in Anne Lot');
var carName;
const maxCars=7;
var parkedCars=[];
parkCar=function (carName){
if(parkedCars.length<=maxCars){
parkedCars.push(carName);
console.log(parkedCars);
console.log("Welcome to Anne's Lot.");
}
else {
console.log("Sorry, Anne's Lot is at max capacity.");
}
}
Then in your console type
parkCar('Car1');
and so on, up to 7 cars.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With