Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I'm using padStart in javascript but it's not working for internet explorer
I would like to know if someone know an alternative to replace this.
Thank you
There's a polyfill at the MDN:
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength,padString) {
targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength-this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0,targetLength) + String(this);
}
};
}
You can copy it in your script.
Related
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 8 months ago.
Improve this question
i have a problem in laravel with links in a fullcalednar script. I need the links to be only with date: 2022-06-28 not with T00:00:00+03:00. i do this but not work any ideea?
// timeZoneOffset is in minutes
function buildIsoString(marker, timeZoneOffset, stripZeroTime) {
if (stripZeroTime === void 0) { stripZeroTime = false; }
var s = marker.toISOString();
s = s.replace('.000', '');
if (stripZeroTime) {
s = s.replace('T00:00:00Z', '');
}
if (s.length > 10) { // time part wasn't stripped, can add timezone info
if (timeZoneOffset == null) {
s = s.replace('Z', '');
}
else if (timeZoneOffset !== 0) {
s = s.replace('Z', formatTimeZoneOffset(timeZoneOffset, true));
}
// otherwise, its UTC-0 and we want to keep the Z
}
return s;
}
I don't fully understand the problem. So, please bear with me.
If you want to strip the hours, minutes, seconds and miliseconds from an ISO string (from Date.toISOString()) you can simply do the following:
Option 1: isoString = isoString.slice(0, 10);
Option 2: isoString = isoString.replace(/T.*$/, "");
I'm not sure this answers your question. I would need to know more about how and where the buildIsoString function is being called and what the formatTimeZoneOffset does.
I hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm creating a website where the user types whatever he hears in an audio file. On submit, I want to compare his input to the actual paragraph and then display the misspelled words. How can I do that?
// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let mistakedWords = [];
for(let i = 0; i < answerParagraph.length; i++){
if(answerParagraph[i] != inputParagraph[i]){
mistakes++
mistakedWords.push(answerParagraph[i])
}
}
alert("You have " + mistakes + " typos!")
I think this should work. :)
Edit: Well if you want to check every word one by one instead of the whole paragraph then the code is:
// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let answerWords = answerParagraph.split(" ")
let inputWords = inputParagraph.split(" ")
for(let i = 0; i < answerWords.length; i++){
if(answerWords[i] != inputWords[i]){
mistakes++
}
}
alert("You have mistakes in " + mistakes + " words!")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm a bit confused as I was asked to simplify this function down to a single return statement, and I'm not exactly sure how I would do that as I'm not sure what the paradigm is for angular / typescript with this.
get remainingSpend() {
if (this.spend >= 0) {
if (this.spend- this.organization.total <= 0) {return '';}
return this.spend - this.organization.total;
} else {
return '';
}
}
You can simplify it with a ternary operator like so:
get remainingSpend(){
return this.spend < 0 || this.spend <= this.organization.total ? ''
: this.spend - this.organization.total
}
get remainingSpend() {
return (this.spend >= 0 && this.spend > this.organization.total) ?
this.spend - this.organization.total : ''
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Is there a js library that takes a nuber >=1 and formats it using vulgar abreviations like 100k$ or 10M$, etc so its possible to shorten numbers in a way everybody understands?
So basicly:
1=>1
100=>100
1354=>1.3K
1586543=>1.5M
4234567895=>4.2B
I don't understand why you need a library to do this? You could just a switch:
const getAbb = (n) => {
const number = n.toString()
const len = number.length
const place = len % 3 || 3
let abb, r
switch(true) {
case len > 9:
abb = 'B'
break
case len > 6:
abb = 'M'
break
case len > 3:
abb = 'K'
break
default:
return number
}
return `${number.slice(0, place)}.${number.slice(place, place + 1)}${abb}`
}
console.log(getAbb(1))
console.log(getAbb(12))
console.log(getAbb(123))
console.log(getAbb(1234))
console.log(getAbb(12345))
console.log(getAbb(123456))
console.log(getAbb(1234567))
console.log(getAbb(12345678))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Trying to figure out this times table function...It's not working as there seems to be a problem with the if statement containing break. Any help would be much appreciated.
function writeTimesTable (startNumber, endNumber, multNumber) {
for (;startNumber <= endNumber; startNumber++) {
console.log(startNumber + " * " + multNumber + " = " + startNumber * multNumber + "</br>")
}
}
/* writeTimesTable(3,4,5) */
var timesTable;
while ( (timesTable = prompt("Enter the times table", -1)) != -1)
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
};
if (timesTable == -1) {
break;
};
console.log("<br />The " + timesTable + " times table<br/>)");
writeTimesTable(timesTable, 1, 12);
you're not using brackets in your outer loop. without brackets only the first statement is executed in the loop.
while ( (timesTable = prompt("Enter the times table", -1)) != -1){
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
}