How to split the number in js [closed] - javascript

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 3 years ago.
Improve this question
I have only one number like "100000"
I wan't to split this number same as [20000,20000,20000,20000,20000] in array
so if number is 138000 then it should be like [20000,20000,20000,20000,20000,20000,18000]
How to do this?

let num = 118000,
num_array = [];
while (num > 0) {
if (num >= 20000) {
num_array.push(20000);
num -= 20000;
} else {
num_array.push(num);
break;
}
}
console.log(num_array);
I think this is what you want, please post what you tried, so that it helps to understand where you failed.

Related

Shouldn't allow user to enter or copy/paste Emoji characters in any fields [closed]

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 4 years ago.
Improve this question
my question is probably simple for most but its troubling me and ive been googling for sometime now.
I dont want allow copy or paste content and emoji characters in input fields.
var checkTf = function() {
let charcode_max = 255;
let tf = document.getElementById("tf");
for (let i = tf.value.length - 1; i >= 0; i--) {
if (tf.value[i].charCodeAt(0) > charcode_max) {
tf.value = tf.value.substr(0, i) + tf.value.substr(i + 1);
}
}
}
<html>
<head></head>
<body>
<textarea id="tf" oninput="checkTf()"></textarea>
</body>
</html>
You may increase charcode_max.
https://jsfiddle.net/qx4dt5bz/

Javascript: find english word in string [closed]

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 7 years ago.
Improve this question
i want to find any english word (min 4 letters) in one string.
Eg: hello123fdwelcome => ["hello", "welcome"];
Could you suggest for me any solution or javascript lib to match english word.
Thanks
You can use this list of words https://github.com/dwyl/english-words
var input = "hello123fdwelcome";
var fs = require('fs');
fs.readFile("words.txt", function(words) {
var words = words.toString().split('\n').filter(function(word) {
return word.length >= 4;
});
var output = [];
words.forEach(word) {
if (input.match(word)) {
output.push(word);
}
});
console.log(output);
});

Find an element that has a text value of 0? [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 7 years ago.
Improve this question
I need to find an element that's text value is 0.
I do:
var d = $('.content').find('div');
if(d.text().length === 0){
//do something
}
Is there a way to put the above logic in the selector?
I've tried:
var d = $('.content').find('div:empty');
But no luck as the div may have other empty html tags in.
Use a filter
var elems = $('.content').find('div').filter(function() { return $(this).text() == 0 });

Returns the cube of the sum of an input and 2 [closed]

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 8 years ago.
Improve this question
I'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.

How to target a specific part of string? [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 9 years ago.
Improve this question
returns the index where pattern starts in string (or -1 if not found).
The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.
Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1
Maybe something like:
var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
//stuff
} else {
//other stuff
}

Categories