-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (116 loc) · 5.11 KB
/
Copy pathscript.js
File metadata and controls
133 lines (116 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Destination {
constructor(name, image, description, recommendations, tripType) {
this.name = name;
this.image = image;
this.description = description;
this.recommendations = recommendations;
this.tripType = tripType;
}
}
class TravelPlanner {
constructor() {
this.destinations = [];
this.tripForm = document.getElementById('trip-form');
this.destinationSelect = document.getElementById('destination');
this.durationInput = document.getElementById('duration');
this.tripTypeSelect = document.getElementById('trip-type');
this.tripResult = document.getElementById('trip-result');
this.loadDestinations(); // Load destinations from JSON
this.attachEventListeners();
}
async loadDestinations() {
try {
const response = await fetch('destinations.json'); // Load data from JSON file
const data = await response.json();
this.destinations = data.map(destinationData => {
return new Destination(
destinationData.name,
destinationData.image,
destinationData.description,
destinationData.recommendations,
destinationData.tripType
);
});
this.populateDestinationSelect(); // Populate destination options after loading data
} catch (error) {
console.error('Error loading destinations:', error);
}
}
populateDestinationSelect() {
this.destinations.forEach((destination, index) => {
const option = document.createElement('option');
option.value = index;
option.textContent = destination.name;
this.destinationSelect.appendChild(option);
});
}
attachEventListeners() {
this.tripForm.addEventListener('submit', (event) => {
event.preventDefault();
this.planTrip();
});
}
planTrip() {
try {
const destinationIndex = this.destinationSelect.value;
const duration = parseInt(this.durationInput.value, 10);
if (isNaN(duration) || duration <= 0) {
throw new Error('Invalid duration. Please enter a valid number of days.');
}
if (destinationIndex < 0 || destinationIndex >= this.destinations.length) {
throw new Error('Invalid destination selection.');
}
const destination = this.destinations[destinationIndex];
const recommendations = destination.recommendations.join(', ');
let tripRecommendations;
switch (destination.tripType) {
case 'Romantic':
tripRecommendations = 'Enjoy romantic dinners, take a boat ride on the Seine.';
break;
case 'Cultural':
tripRecommendations = 'Visit museums, explore historical sites.';
break;
case 'City Exploration':
tripRecommendations = 'Explore the city, visit landmarks.';
break;
case 'Historical':
tripRecommendations = 'Visit historical sites, learn about the past.';
break;
case 'Scenic':
tripRecommendations = 'Enjoy scenic views, relax by the waterfront.';
break;
case 'Adventure':
tripRecommendations = 'Go on outdoor adventures, explore nature.';
break;
case 'Luxury':
tripRecommendations = 'Indulge in luxury experiences, stay in upscale hotels.';
break;
case 'Relaxation':
tripRecommendations = 'Relax on the beach, get spa treatments.';
break;
default:
tripRecommendations = 'Explore the destination at your own pace.';
}
// Update the background image URL dynamically based on the selected destination's image
document.body.style.backgroundImage = `url('${destination.image}')`;
this.tripResult.innerHTML = `
<h3>Your Trip Plan:</h3>
<img src="${destination.image}" alt="${destination.name} Image" style="width: 300px; height: 200px;">
<p>Destination: ${destination.name}</p>
<p>Duration: ${duration} days</p>
<p>Description: ${destination.description}</p>
<p>Recommendations: ${recommendations}</p>
<p>Trip Type: ${destination.tripType}</p>
<p>Trip Recommendations: ${tripRecommendations}</p>
`;
this.tripResult.classList.add('show');
} catch (error) {
this.tripResult.textContent = 'Error: ' + error.message;
} finally {
this.tripTypeSelect.value = 'Any';
}
}
}
document.addEventListener('DOMContentLoaded', () => {
const travelPlanner = new TravelPlanner();
});