Split string with JavaScript - javascript

how to parse this string with java script
19 51 2.108997
20 47 2.1089
like this
<span>19 51</span> <span>2.108997</span>
<span>20 47</span> <span>2.1089</span>

Assuming you're using jQuery..
var input = '19 51 2.108997\n20 47 2.1089';
var lines = input.split('\n');
var output = '';
$.each(lines, function(key, line) {
var parts = line.split(' ');
output += '<span>' + parts[0] + ' ' + parts[1] + '</span><span>' + parts[2] + '</span>\n';
});
$(output).appendTo('body');

Like this:
var myString = "19 51 2.108997";
var stringParts = myString.split(" ");
var html = "<span>" + stringParts[0] + " " + stringParts[1] + "</span> <span>" + stringParts[2] + "</span";

var wrapper = $(document.body);
strings = [
"19 51 2.108997",
"20 47 2.1089"
];
$.each(strings, function(key, value) {
var tmp = value.split(" ");
$.each([
tmp[0] + " " + tmp[1],
tmp[2]
], function(key, value) {
$("<span>" + value + "</span>").appendTo(wrapper);
});
});

Related

Google Ads scripts - how to do customised or long rolling date ranges

I'm using the following script to export Google Ads data from my account, but I am stuck with Google's pre-set date ranges and can't figure out how/if it's possible to jimmy these. Ideal date range would be year to date, with the report refreshing each day and adding on a fresh day's data, but would also be interested if we can get all time to work.
I'm a coding novice, so apologies in advance.
Script:
function main() {
var currentSetting = new Object();
currentSetting.ss = SPREADSHEET_URL;
// Read Settings Sheet
var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
var rows = settingsSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
var sortString = "";
var filters = new Array();
for(var i = 0; i < numRows; i++) {
var row = values[i];
var settingName = row[0];
var settingOperator = row[1];
var settingValue = row[2];
var dataType = row[3];
debug(settingName + " " + settingOperator + " " + settingValue);
if(settingName.toLowerCase().indexOf("report type") != -1) {
var reportType = settingValue;
} else if(settingName.toLowerCase().indexOf("date range") != -1) {
var dateRange = settingValue;
} else if(settingName.toLowerCase().indexOf("sort order") != -1) {
var sortDirection = dataType || "DESC";
if(settingValue) var sortString = "ORDER BY " + settingValue + " " + sortDirection;
var sortColumnIndex = 1;
}else {
if(settingOperator && settingValue) {
if(dataType.toLowerCase().indexOf("long") != -1 || dataType.toLowerCase().indexOf("double") != -1 || dataType.toLowerCase().indexOf("money") != -1 || dataType.toLowerCase().indexOf("integer") != -1) {
var filter = settingName + " " + settingOperator + " " + settingValue;
} else {
if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + ' "' + settingValue + '"';
} else if(settingValue.indexOf("'") != -1) {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
} else {
var filter = settingName + " " + settingOperator + " '" + settingValue + "'";
}
}
debug("filter: " + filter)
filters.push(filter);
}
}
}
// Process the report sheet and fill in the data
var reportSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(REPORT_SHEET_NAME);
var rows = reportSheet.getDataRange();
var numRows = rows.getNumRows();
var numCols = rows.getNumColumns();
var values = rows.getValues();
var numSettingsRows = numRows - 1;
// Read Header Row and match names to settings
var headerNames = new Array();
var row = values[0];
for(var i = 0; i < numCols; i++) {
var value = row[i];
headerNames.push(value);
//debug(value);
}
if(reportType.toLowerCase().indexOf("performance") != -1) {
var dateString = ' DURING ' + dateRange;
} else {
var dateString = "";
}
if(filters.length) {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + ' WHERE ' + filters.join(" AND ") + dateString + " " + sortString;
} else {
var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + dateString + " " + sortString;
}
debug(query);
var report = AdWordsApp.report(query);
try {
report.exportToSheet(reportSheet);
var subject = "Your " + reportType + " for " + dateRange + " for " + AdWordsApp.currentAccount().getName() + " is ready";
var body = currentSetting.ss + "<br>You can now add this data to <a href='https://www.optmyzr.com'>Optmyzr</a> or another reporting system.";
MailApp.sendEmail(EMAIL_ADDRESSES, subject, body);
Logger.log("Your report is ready at " + currentSetting.ss);
Logger.log("You can include this in your scheduled Optmyzr reports or another reporting tool.");
} catch (e) {
debug("error: " + e);
}
}
function debug(text) {
if(DEBUG) Logger.log(text);
}
I've tried overwriting the data validation in the host spreadsheet, but think I need to amend the script itself also.

Return all Rows that meet Criteria

hy guys im learning google apps script to telegram bot,
i have write auto reply command and it success to reply, if i sent #02-02-2021 it will show all recorded data on that day, but on my code it just return only 1 row, i have trying anything but nothing work.
thanks for helping,
function searchDataByTanggalTransaksi(Tanggal){
var rangeNameTransaksi = "Transaksi!A2:O";
var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi).values;
var panjangTransaksi = rowsTransaksi.length;
var Tanggal, Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total = "";
var pesan ="";
for (var row = 0; row < panjangTransaksi; row++ ){
if ("#" + rowsTransaksi[row][1]==Tanggal){
Tanggal = "📅 Tanggal Transaksi : " + rowsTransaksi[row][1];
Nama_Item = rowsTransaksi[row][2];
Divisi = rowsTransaksi[row][3];
Sat = rowsTransaksi[row][4];
Qty = rowsTransaksi[row][5];
Harga_Satuan = rowsTransaksi[row][6];
Debet = rowsTransaksi[row][7];
Kredit = rowsTransaksi[row][8];
Saldo = rowsTransaksi[row][9];
Peruntukan = rowsTransaksi[row][12];
Total = "💸 Total Transaksi : " + rowsTransaksi[row][14] + ",-";
pesan += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit + "</code>";
return pesan ;
}
}
return "Data tanggal tidak ditemukan";
}
function testgetRowsTransaksi(){
var tanggal = searchDataByTanggalTransaksi("#2");
var x = ""
}
One approach is to store found values in array, I assume that you want to return pesan right?
So solution one of possible solutions is that every record that is found you store it into array, and then return whole array(if nothing found array length will be 0).
Here is there modified code:
function searchDataByTanggalTransaksi(Tanggal) {
var rangeNameTransaksi = "Transaksi!A2:O";
var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi)
.values;
var panjangTransaksi = rowsTransaksi.length;
var Tanggal,
Nama_Item,
Divisi,
Sat,
Qty,
Harga_Satuan,
Debet,
Kredit,
Saldo,
Peruntukan,
Total = "";
var pesan = "";
var resultArray = []; //Mentioned array to pass data in.
for (var row = 0; row < panjangTransaksi; row++) {
if ("#" + rowsTransaksi[row][1] == Tanggal) {
Tanggal = "📅 Tanggal Transaksi : " + rowsTransaksi[row][1];
Nama_Item = rowsTransaksi[row][2];
Divisi = rowsTransaksi[row][3];
Sat = rowsTransaksi[row][4];
Qty = rowsTransaksi[row][5];
Harga_Satuan = rowsTransaksi[row][6];
Debet = rowsTransaksi[row][7];
Kredit = rowsTransaksi[row][8];
Saldo = rowsTransaksi[row][9];
Peruntukan = rowsTransaksi[row][12];
Total = "💸 Total Transaksi : " + rowsTransaksi[row][14] + ",-";
pesan +=
"<code>- " +
Nama_Item +
" |" +
Qty +
" " +
Sat +
" |" +
Kredit +
"</code>";
return resultArray.push(pesan); //Data passed in per every iteration.
}
}
return resultArray; //returning line
return "Data tanggal tidak ditemukan"; // this line is obsolete
}
function testgetRowsTransaksi() {
var tanggal = searchDataByTanggalTransaksi("#2");
var x = "";
}
Though I'm not pretty sure, what kind of data panjangTransaksi variable holds, if you just want the all matching items in a concatenated string, you could try like below
function searchDataByTanggalTransaksi(Tanggal){
var rangeNameTransaksi = "Transaksi!A2:O";
var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi).values;
var panjangTransaksi = rowsTransaksi.length;
return panjangTransaksi.reduce((acc, ele) => {
let Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total = "";
if ("#" + ele[1] == Tanggal){
Tanggal = "📅 Tanggal Transaksi : " + ele[1];
Nama_Item = ele[2];
Divisi = ele[3];
Sat = ele[4];
Qty = ele[5];
Harga_Satuan = ele[6];
Debet = ele[7];
Kredit = ele[8];
Saldo = ele[9];
Peruntukan = ele[12];
Total = "💸 Total Transaksi : " + ele[14] + ",-";
acc += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit + "</code>";
}
return acc;
}, '') || "Data tanggal tidak ditemukan";
}
The program will never enter this if ("#" + rowsTransaksi[row][1]==Tanggal) as Tanggal is undefined.
i have found the work around, on above method i was use return, but when i sendText it reply all the rows
case "/tgl" :
var ssIdLogistik ="mySsId";
var rangeNameTransaksi = "Transaksi!A2:O";
var rowsTransaksi = Sheets.Spreadsheets.Values.get(ssIdLogistik, rangeNameTransaksi).values;
var panjangTransaksi = rowsTransaksi.length;
var Tanggal, Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total = "";
for ( var row = 0; row < panjangTransaksi; row++){
if (rowsTransaksi[row][1] == perintah[1]){
Tanggal = "📅 Tanggal Transaksi : " + rowsTransaksi[row][1];
Nama_Item = rowsTransaksi[row][2];
Divisi = rowsTransaksi[row][3];
Sat = rowsTransaksi[row][4];
Qty = rowsTransaksi[row][5];
Harga_Satuan = rowsTransaksi[row][6];
Debet = rowsTransaksi[row][7];
Kredit = rowsTransaksi[row][8];
Saldo = rowsTransaksi[row][9];
Peruntukan = rowsTransaksi[row][12];
Total = "💸 Total Transaksi : " + rowsTransaksi[row][14] + ",-";
text += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit+ ",-" + "</code>\n";
}
}
if(text == ""){
text = "🚫 Periksa format penulisan kode permintaan Tanggal.\n\n" +
"Melihat transaksi berdasarkan tanggal di awali dengan '/tgl_' dengan format /tgl_hh-bb-tttt:\n" +
" 👉🏻 /tgl_02-02-2021 \n";}
sendText(updates.message.chat.id, logoData + "<code>" + Tanggal + "\n----------\n" + text + "----------\n" + Total + "</code>");
break;

Matching a String with Array of String in Java Script

str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
if (str1 = array B){
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'B000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array C) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'C000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array E) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'E000'
print ('Comm_Code = ' + comm_code + '\n')
}
else if (str1 = array F) {
print ('Prefix , first 3 digit = ' + str1 + '\n')
comm_code = 'F000'
print ('Comm_Code = ' + comm_code + '\n')
}
else {
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n')
comm_code = 'D000'
print ('Comm_Code = ' + comm_code + '\n')
}
Hello,
I want to know how to match the string Str1 with the value of the Array B,C,E,F.
I mean :
If Str1 = 800|| 807 || 826 || 847 || 866, Then Comm_code = B000
If Str1 = 827 || 846 then Comm_code = C000
If Str1 = 867 || 879 then Comm_code = E000
If Str1 = 880 || 899 then Comm_code = F000
Else Default --> Comm_code = D000
Please kindly advice.
p.s. : Fyi, I'm using EcmaScript 2015 / ES5.
Just use a simple String.prototype.indexOf:
str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
if (B.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'B000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (C.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'C000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (E.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'E000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (F.indexOf(str1) > -1)
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'F000';
print ('Comm_Code = ' + comm_code + '\n');
}
else
{
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
comm_code = 'D000';
print ('Comm_Code = ' + comm_code + '\n');
}
You can achieve this with simple if else conditions using Array.indexOf() Methods. However make sure the str1 and the values in the array are of same variable type(string or a number).
if (B.indexOf(str1) > -1 ) { //if value exists in B
//do soemthing;
}
else if(C.indexof(str1) >-1 ) { //if value exists in C
//do soemthing
}
One possible solution to solve this is use Array.some() (I think it is included on ES5 based on this link). First you can create a method that will check if an element is on an array:
function arrayIncludes(arr, ele)
{
return arr.some(function(x) {return (x === ele);});
}
console.log(arrayIncludes([1,2], 2));
console.log(arrayIncludes([1,2], 5));
.as-console {background-color:black !important; color:lime;}
Then, your code can be reworked to this:
str1 = booking_kode.substring(0, 3);
B = ["800", "807", "826", "847", "866"];
C = ["827", "846"];
E = ["867", "879"];
F = ["880", "899"];
function arrayIncludes(arr, ele)
{
return arr.some(function(x) {return (x === ele);});
}
if (arrayIncludes(B, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'B000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(C, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'C000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(E, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'E000';
print ('Comm_Code = ' + comm_code + '\n');
}
else if (arrayIncludes(F, str1))
{
print ('Prefix , first 3 digit = ' + str1 + '\n');
comm_code = 'F000';
print ('Comm_Code = ' + comm_code + '\n');
}
else
{
print ('Prefix , Nilai 3 digit pertama = ' + str1 + '\n');
comm_code = 'D000';
print ('Comm_Code = ' + comm_code + '\n');
}

Generate random equations with random numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to generate random equations in JavaScriptp and then output them into an HTML tag.
This is the code:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return function() {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
}
function getRandomNumber(results) {
var rollDie = getRandomizer( 1, 10 );
for ( var i = 0; i < 3; i++ ) {
results += rollDie() + "";
}
getRandomNumber.result = results;
}
function getRandomEquation(num1, num2, num3, num4, num5, num6, num7, output) {
var num_7,
num_6,
num_5,
num_4,
num_3,
num_2,
num_1
getRandomNumber(num1).result = num_7;
getRandomNumber(num2).result = num_6;
getRandomNumber(num3).result = num_5;
getRandomNumber(num4).result = num_4;
getRandomNumber(num5).result = num_3;
getRandomNumber(num6).result = num_2;
getRandomNumber(num7).result = num_1;
var equation1 = "" + num_1 + " x " + num_2 + " + {" + num_3 + " x [(" + num_4 + " x " + num_5 + ") - " + num_6 + "] + " + num_7 + "} = x",
equation2 = "" + num_1 + " x " + num_2 + " = y",
equation3 = "" + num_1 + "s x " + num_2 + " = z, s = " + num_3,
equation4 = "" + num_1 + " + {" + num_2 + " x [" + num_3 + " + (" + num_4 + " x " + num_5 + ") + " + num_6 + "] + " + num_7 + "} = x",
equation5 = "" + num_1 + "e + " + num_2 + "l x " + num_3 + " + " + num_4 + "a, e = " + num_5 + ", l = " + num_6 + ", a = " + num_7,
equation6 = "[" + num_1 + " x " + num_2 + "z] + {" + num_3 + " - " + num_4 + "} + (" + num_5 + " + " + num_6 + ") = e, z = " + num_7,
equation7 = "p" + " x " + num_1 + " / " + num_2 + " - " + num_3 + " + " + num_4 + " = e, p = " + num_5
var values = [
// there is an easier way to do this, too lazy
"" + equation1,
"" + equation2,
"" + equation3,
"" + equation4,
"" + equation5,
"" + equation6,
"" + equation7
]
var i = 0;
var e;
if (i > values.length) {
i = 0;
}
var randomEquation = values[i];
i++;
e = values[i];
this.output = randomEquation;
this.e = e;
}
function getEquation() {
var bl1,
bl2,
bl3,
bl4,
bl5,
bl6,
bl7,
equationOutput;
var eq = getRandomEquation(bl1, bl2, bl3, bl4, bl5, bl6, bl7, equationOutput).e;
getEquation.equation = eq;
}
function change() {
var final = getEquation().equation;
document.getElementById("generate").innerHTML = final;
}
</script>
</body>
</html>
But it dosen't work. Any help?
P.S. My teacher assigned this to me. Please respond as soon as possible. Thanks.
This code is a complete mess. I dont know where it comes from, but definitely not Javascript.
Try the following instead:
<!DOCTYPE html>
<body>
<p>Click the button below to generate a random equation.</p>
<button onclick="change();">Generate</button>
<p id="generate"></p>
<script>
function getRandomizer(bottom, top) {
return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
}
function getRandomNumber() {
var results="";
for ( var i = 0; i < 3; i++ ) {
results += getRandomizer( 1, 10 );
}
return results;
}
function getRandomEquation() {
var num_7 = getRandomNumber(),
num_6 = getRandomNumber(),
num_5 = getRandomNumber(),
num_4 = getRandomNumber(),
num_3 = getRandomNumber(),
num_2 = getRandomNumber(),
num_1 = getRandomNumber();
var equation1 = num_1+" x "+num_2+" + {"+num_3+" x [("+num_4+" x "+num_5+") - "+num_6+"] + "+num_7+"} = x",
equation2 = num_1+" x "+num_2+" = y",
equation3 = num_1+"s x "+num_2+" = z, s = "+num_3,
equation4 = num_1+" + {" +num_2+ " x [" +num_3+" + ("+num_4+" x "+num_5+") + "+num_6+"] + "+num_7+"} = x",
equation5 = num_1+"e + "+num_2+"l x "+num_3+" + "+num_4+"a, e = "+num_5+", l = "+num_6+", a = "+ num_7,
equation6 = "["+num_1+" x "+num_2+ "z] + {"+num_3+" - "+num_4+"} + ("+num_5+" + "+num_6+") = e, z = "+ num_7,
equation7 = "p x "+num_1+" / "+num_2+" - "+num_3+" + "+num_4+" = e, p = "+num_5
var randomEquation = [
equation1,
equation2,
equation3,
equation4,
equation5,
equation6,
equation7
]
return randomEquation.join("<br>");
}
function change() {
document.getElementById("generate").innerHTML = getRandomEquation();
}
</script>
</body>
</html>
Well, you could do this:
var type = (Math.floor(Math.random(4));
var ret = 0;
var n = [(Math.floor(Math.random(100)), (Math.floor(Math.random(100))];
if (type == 0) ret = n[0] + n[1]
else if (type == 1) ret = Math.abs(n[0] - n[1])
else if (type == 2) ret = n[0] * n[1];
else if (type == 3) ret = n[0] / n[1]
else ret = n[0] / 5 % n[1]
// do something with ret
It's fully expandable, just edit n and the if statements

I have an error and for some reason the numbers are being changed and not the names?

This is the question that I need help on.
I need to have the names change randomly. I also need the names to be the same for both selection.
If you notice, it is possible to have the two random names be the same. I made some error and I need to fix this so that the first and second names are unique. I wanted to pass the first name generated to the getoption function of the second name so that second name cannot be this name. This is what i am having trouble with can someone please help me?
So far I have the code to make the numbers change but I'm not sure how to randomize the names
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney + ". Mary buys a " + marystuff + " for $" + maryitem + " and John buys a " + +" for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);
Try this, it works for me:
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoptions(s, ch) {
var a = s.split(ch);
a.sort( function() { return 0.5 - Math.random() } );
return a;
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var stuff = "notebook,pencil,ruler,pen,eraser,binder,backpack";
var options = getoptions(stuff, ',');
var marystuff = options[0];
var johnstuff = options[1];
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney +
". Mary buys a " + marystuff + " for $" + maryitem +
" and John buys a " + johnstuff + " for $" + johnitem +
". They have $" + totalleft + ".";
document.write(str);
You can add the following to make sure the item names are not the same:
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
You are also missing johnstuff from your str at the end.
Try running the code below to see that it works:
var person1, person2;
function getPeople() {
var people = ['Mary', 'John', 'Misa', 'Steve', 'Amy', 'David'];
person1 = people[Math.floor(Math.random() * people.length)];
person2 = people[Math.floor(Math.random() * people.length)];
while (person1 === person2) {
person2 = people[Math.floor(Math.random() * people.length)];
}
}
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random());
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
getPeople();
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = person1 + " had $" + marymoney + " and " + person2 + " had $" + johnmoney + ". " + person1 + " buys a " + marystuff + " for $" + maryitem + " and " + person2 + " buys a " + johnstuff + " for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);

Categories