In: Computer Science
/* Complete this javascript file according to the individual instructions given in the comments. */ // 1) Declare an array named myArray // Assign myArray three elements: 'Tuesday',3,true // Print myArray to the console with console.log() // 2) Use typeof on each element in myArray // Example: typeof myArray[0]; // Print all 3 lines to the console with console.log() // 3) Declare a variable named myStart and set it to // the value of the length property applied of myArray // Print the result to the console with console.log() // 4) Use the .splice() method on myArray // Use your variable myStart - 2 as the first parameter of .splice() // Use 0 as the second parameter // In the third parameter, specify 'hamburgers' as the element to add // Print the resulting array to the console with console.log() // 5) Declare an array named myPal // Give myPal these 4 elements: 'taco','cat','race','car' // Set a string equal the first element + the second element // Print the string to the console with console.log() // 6) Use the .concat() method to merge myArray and myPal // Print the result to the console with console.log() // 7) Use the .join() method on myPal and specify no connector parameter // Print the result to the console with console.log() // 8) Use .sort() on myArray // Print the resulting array to the console with console.log() // 9) Use .slice() on myPal and specify that it begins at // myPal.length - 2 // Print the result to the console // 10) **Solving a problem! // Declare a string named revOrder that is equal to 'Taco cat' // Utilize indexOf(), split(), splice(), reverse(), and join() to // 1) turn the string into an array // 2) locate and remove the empty space // 3) Reverse the order of the array // 4) Turn the array back into a string // 5) Log each step to the console. // Can you combine any of the steps into one statement? // You should finish with a result of "tacocaT"
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new web page with name "jsArrayDemo.html" is created, which contains following code.
jsArrayDemo.html :
<!DOCTYPE html>
<html lang="en">
<head>
<!-- title for web page -->
<title>javascript Array</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- <script> is used for javascript -->
<script>
// 1) Declare an array named myArray
// Assign myArray three elements: 'Tuesday',3,true
// Print myArray to the console with console.log()
var myArray=['Tuesday',3,true];
//using for loop ,print each array element
for(var i=0;i<myArray.length;i++){
console.log("myArray["+i+"] : "+myArray[i]);
}
// 2) Use typeof on each element in myArray
// Example: typeof myArray[0];
// Print all 3 lines to the console with console.log()
for(var i=0;i<myArray.length;i++){
console.log("typeof(myArray["+i+"]) : "+typeof(myArray[i]));
}
// 3) Declare a variable named myStart and set it to
// the value of the length property applied of myArray
// Print the result to the console with console.log()
var myStart=myArray.length;
console.log("myArray length : "+myStart);
// 4) Use the .splice() method on myArray
// Use your variable myStart - 2 as the first parameter of .splice()
// Use 0 as the second parameter
// In the third parameter, specify 'hamburgers' as the element to add
// Print the resulting array to the console with console.log()
myArray.splice(myStart - 2 ,0,"hamburgers");
for(var i=0;i<myArray.length;i++){
console.log("myArray["+i+"] : "+myArray[i]);
}
// 5) Declare an array named myPal
// Give myPal these 4 elements: 'taco','cat','race','car'
// Set a string equal the first element + the second element
// Print the string to the console with console.log()
var myPal=['taco','cat','race','car'];
console.log(myPal[0]+myPal[1]);
// 6) Use the .concat() method to merge myArray and myPal
// Print the result to the console with console.log()
myArray=myArray.concat(myPal);
for(var i=0;i<myArray.length;i++){
console.log("myArray["+i+"] : "+myArray[i]);
}
// 7) Use the .join() method on myPal and specify no connector parameter
// Print the result to the console with console.log()
console.log(myPal.join());
// 8) Use .sort() on myArray
// Print the resulting array to the console with console.log()
myArray.sort();
for(var i=0;i<myArray.length;i++){
console.log("myArray["+i+"] : "+myArray[i]);
}
// 9) Use .slice() on myPal and specify that it begins at
// myPal.length - 2
// Print the result to the console
console.log(myPal.slice(myPal.length - 2));
// 10) **Solving a problem!
// Declare a string named revOrder that is equal to 'Taco cat'
// Utilize indexOf(), split(), splice(), reverse(), and join() to
// 1) turn the string into an array
// 2) locate and remove the empty space
// 3) Reverse the order of the array
// 4) Turn the array back into a string
// 5) Log each step to the console.
// Can you combine any of the steps into one statement?
// You should finish with a result of "tacocaT"
var revOrder="Taco cat";
var revOrderArray=revOrder.split(" "); //reversing array
for(var i=0;i<revOrderArray.length;i++){
console.log("revOrderArray["+i+"] : "+revOrderArray[i]);
}
var removeIndex=revOrder.indexOf(" ");
console.log(removeIndex);
//array reverse
var arrayReverse=revOrderArray.reverse();
for(var i=0;i<revOrderArray.length;i++){
console.log("revOrderArray["+i+"] : "+revOrderArray[i]);
}
//array join
var arrayJoin=arrayReverse.join(" ");
console.log("String is : "+arrayJoin);
</script>
</body>
</html>
======================================================
Output : Open web page jsArrayDemo.html in the browser and will get the screen as shown below
Screen 1 :jsArrayDemo.html
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.