Last active
December 12, 2017 19:59
-
-
Save BlazingAsher/ed80b84fbde6673cae10002383a39935 to your computer and use it in GitHub Desktop.
Arrays Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Question 1 | |
/* | |
var items,costs,item,cost,totalCost,percent; | |
//Initializing Variables | |
items = []; | |
costs = []; | |
totalCost=0; | |
do{//Add items to array | |
item = prompt("Enter an item. Enter STOP to stop.").toUpperCase(); | |
if (item != "STOP"){ | |
cost = parseFloat(prompt("Enter the price for "+item)); | |
items.push(item); | |
costs.push(cost); | |
totalCost+=cost; | |
} | |
}while(item != "STOP"); | |
//Start table | |
document.write('<table border="1"><tr><th>Item</th><th>Percentage</th></tr>'); | |
for(i=0;i<items.length;i++){//Iterate array and print to table | |
percent=(costs[i]/totalCost)*100; | |
percent=percent.toFixed(2); | |
document.write("<tr><td>",items[i],"</td><td>",percent,"%</td></tr>"); | |
} | |
document.write("</table>"); | |
*/ | |
//Question 2 | |
//The first block of code is using JS Split, while the other is a custom-made split. | |
/* | |
var word,wordArr,ind; | |
//Initializing variables | |
word = prompt("Enter a word"); | |
for (i=0;i<word.length;i++){//Remove commas | |
if (word.charAt(i) == ","){ | |
word = word.slice(0,i) + word.slice(i+1); | |
} | |
} | |
wordArr = word.split(" "); | |
document.write(wordArr); | |
*/ | |
/* | |
//Homemade Split! | |
var word,wordArr,wordLittle; | |
//Initialize variables | |
word = prompt("Enter a word"); | |
wordArr = []; | |
wordLittle = ""; | |
//Start iterating array | |
for(i=0;i<word.length;i++){ | |
if (word.charAt(i) == " "){//Check if there is a space | |
wordArr.push(wordLittle); | |
wordLittle = ""; | |
} | |
else if (word.charAt(i) == ","){//Check if there is a comma | |
} | |
else{ | |
wordLittle=wordLittle+word.charAt(i);//Add character to temp word | |
} | |
if(i==word.length-1){//If at end, push the rest to array | |
wordArr.push(wordLittle); | |
} | |
} | |
document.write(wordArr); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment