-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfchcdf.js
More file actions
188 lines (149 loc) · 5.49 KB
/
nfchcdf.js
File metadata and controls
188 lines (149 loc) · 5.49 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// A simple task list in JavaScript
// Create an empty array to hold all the tasks addeds
const tasks = [];
// Make a function to display the current task list that is added
function displayTasks() {
if (tasks.length === 0) {
console.log("No tasks to display.");
} else {
console.log("Current tasks:");
tasks.forEach((task, index) => {
console.log(`${index + 1}. ${task}`);
});
}
}
// Create a function to add a task to the end of the task list
function addTask() {
const newTask = prompt("Enter a new task:");
tasks.push(newTask);
console.log(`"${newTask}" has been added to the task list.`);
}
// Create a function to remove a task from the beginning of the task list
function removeFirstTask() {
if (tasks.length === 0) {
console.log("No tasks to remove.");
} else {
const removedTask = tasks.shift();
console.log(`"${removedTask}" has been removed from the beginning of the task list.`);
}
}
// Create a function to remove a task from the end of the task list
function removeLastTask() {
if (tasks.length === 0) {
console.log("No tasks to remove.");
} else {
const removedTask = tasks.pop();
console.log(`"${removedTask}" has been removed from the end of the task list.`);
}
}
// Create a function to remove a task from a specific position in the task list
function removeTask() {
if (tasks.length === 0) {
console.log("No tasks to remove.");
} else {
const index = parseInt(prompt("Enter the index of the task to remove:")) - 1;
if (index < 0 || index >= tasks.length) {
console.log("Invalid index.");
} else {
const removedTask = tasks.splice(index, 1)[0];
console.log(`"${removedTask}" has been removed from the task list.`);
}
}
}
// Going back to the main program loop
let choice;
do {
console.log("\nChoose an option:");
console.log("1. Add a task");
console.log("2. Remove a task from the beginning");
console.log("3. Remove a task from the end");
console.log("4. Remove a task from a specific position");
console.log("5. Display the task list");
console.log("6. Quit");
choice = parseInt(prompt("Enter your choice (1-6):"));
switch (choice) {
case 1:
addTask();
break;
case 2:
removeFirstTask();
break;
case 3:
removeLastTask();
break;
case 4:
removeTask();
break;
case 5:
displayTasks();
break;
case 6:
console.log("Goodbye!");
break;
default:
console.log("Invalid choice.");
}
} while (choice !== 6);
//////////////////////////////////////////////////
Create a program that calculates the sum of all the elements in a given array of numbers. You can use any of the iteration methods discussed above to traverse
the array elements and accumulate the sum. For example, if the array is [1, 2, 3, 4, 5], the program should output 15 as the sum. This challenge will help you
practice array iteration and working with the length property.
// Create a const with a sample array of numbers
const numbers = [1, 2, 3, 4, 5];
// Start the initial counting from 0
let sum = 0;
// Iterate through each element in the array and add to sum
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
// Print out the output of the code
console.log(sum);
/////////////////////////////////////////////////////////////////////
Create a program that calculates the total of all the numbers inside the nested array.
let nestedArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Write your code here to calculate the total of all numbers in the nestedArray
let nestedArray = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// In this code, we define a new variable nestedArray and assign it an array of three arrays.
// Each of the three inner arrays has three numbers. This creates a nested array structure, which
// can be thought of as an array of arrays.
let total = 0;
// Here, we define a new variable total and initialize it to zero. This variable will be used to accumulate the sum of all the numbers in the nested array.
for (let i = 0; i < nestedArray.length; i++) {
for (let j = 0; j < nestedArray[i].length; j++) {
total += nestedArray[i][j];
}
}
This is the main part of the code where we calculate the total of all the numbers in the nested array. We use two nested for loops to iterate over each element
of the nested array. The outer loop iterates over the outer array, while the inner loop iterates over each inner array.
For each inner array, we use the inner loop to iterate over each element of that array. We then add each element to the total variable using the += operator,
which is a shorthand way of writing total = total + nestedArray[i][j].
Finally, after all the iterations are complete, the total variable will contain the sum of all the numbers in the nested array.
console.log(total);
// This line logs the total variable to the console, so we can see the final result of our calculation.
//DOM practice
<div id="container">
<p>First</p>
<p>Second</p>
<p>Third</p>
</div>
//DOM tracerse
<script type="text/javascript">
let container = document.getElementById("container");
let firstParagraph = container.firstChild;
let secondParagraph = firstParagraph.nextSibling;
let thirdParagraph = secondParagraph.nextSibling;
</script>
// DOM tree - various ways
let container = document.getElementById("container");
let newParagraph = document.createElement("p");
let textNode = document.createTextNode("New paragraph");
newParagraph.appendChild(textNode);
container.appendChild(newParagraph);